0

ボタンにテキストのシャッフルを実装するにはどうすればよいですか。つまりtext = chr(n+48)、各ボタンのテキストをシャッフルするにはどうすればよいですか。

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
4

1 に答える 1

0

それを2つのステップで行う理由があるかどうかはわかりません。
次のようなものを試してください。

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
于 2012-11-13T12:11:24.860 に答える