2012-11-13 45 views
0

如何在我的按鈕上實現對文本的混洗,即text = chr(n+48)以混洗每個按鈕上的文本。Shuffling按鈕文本

Dim n As Integer = 0 

For i As Integer = 0 To 10 
    ' Initialize one variable 
    btnArray(i) = New Button 

Next i 

While n < 10 
    With btnArray(n) 
     .Tag = n + 1 ' Tag of button 
     .Width = 40 ' Width of button 
     .Height = 40 
     .Text = Chr(n + 48) 
     FlowLayoutPanel1.Controls.Add(btnArray(n)) 
     AddHandler .Click, AddressOf Me.GenericClickHandler 
     n = n + 1 
    End With 
End While 
+0

問題是什麼錯誤? – Nianios

+0

Dim r As New Random Dim out =(from b在btnArray Order by r.Next選擇b).ToArray()我已經這樣做了洗牌ARRAY,但它從不在顯示面板上顯示整個9個按鈕。它只顯示3/4/5或表單上的空白按鈕。我想我錯了代碼。我正在像這樣做flowlayoutpanel1.controls.add(out(n))。它給出了錯誤的輸出。 –

回答

0

我不知道是否有一個原因,你是這樣做的兩個步驟。
嘗試是這樣的:根據您的意見

Private btnArray As New List(Of Button) 

For i As Integer = 0 To 10 
    Dim btn As New Button 
    With btn 
     .Tag = i ' Tag of button 
     .Width = 40 ' Width of button 
     .Height = 40 
     .Text = Chr(i + 48) 

    End With 
    btnArray.Insert(i, btn) 

    'FlowLayoutPanel1.Controls.Add(btnArray(i)) 'It works also 
    FlowLayoutPanel1.Controls.Add(btn) 
    AddHandler .Click, AddressOf Me.GenericClickHandler 

Next i 

Private btnArray As New List(Of Button) 
Private btnShuffleArray As New List(Of Button) 

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    For i As Integer = 0 To 10 
     ' Initialize one variable 
     Dim btn As New Button 
     With btn 
      .Tag = i ' Tag of button 
      .Width = 40 ' Width of button 
      .Height = 40 
      .Text = Chr(i + 48) 
      btnArray.Insert(i, btn) 
      ' FlowLayoutPanel1.Controls.Add(btnArray(i)) 
      'AddHandler .Click, AddressOf Me.GenericClickHandler 
     End With 
    Next i 

    'Randomize the list 
    Dim rand As New Random 
    Dim index As Integer 
    While btnArray.Count > 0 
     index = rand.Next(0, btnArray.Count) 
     btnShuffleArray.Add(btnArray(index)) 
     btnArray.RemoveAt(index) 
    End While 

    For i = 0 To 10 
     FlowLayoutPanel1.Controls.Add(btnShuffleArray(i)) 
    Next 

End Sub