2012-11-13 44 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