且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在vb.net中创建按钮控件数组?!

更新时间:2023-09-23 13:57:16

CMember 7655557写道:
CMember 7655557 wrote:

每个按钮的文字是从0到99的数字。我想使用For ... Next循环在按钮数组的每个按钮的文本上添加0到99之间的数字。

The Text of each button is a number which from 0 to 99. I'd like to use the For...Next loop to add number which from 0 to 99 on the text of each button of button array.





澄清后,我建议使用以下代码。你不需要数组来做到这一点。





After your clarification, I would suggest to use following code. You don't need array to do that.

'Dim buttons() As Button


For i = 1 To 100
  Dim btns() As Button = Controls.Find("Button" & i, True)
  Dim btn As Button
  If btns IsNot Nothing Then
    btn = btns(0)
    'If buttons Is Nothing Then
    '  ReDim buttons(0)
    'Else
    '  ReDim Preserve buttons(buttons.Length)
    'End If
    'buttons(UBound(buttons)) = btn
    btn.Text = i - 1 'here you can change whatever you want
  End If
Next

>

尝试Followinn链接



动态创建数组



这个懒散的编程亲爱的尝试goog le search。
Tries Followinn links

Dynamic creatio nof array

This lasy programming dear tries google search.


步骤1,创建一个名为Form1的表单。

步骤2,添加一个名为TableLayoutPanel1的TableLayoutPanel。

步骤3,将其设为10 X 10,每个10%。

步骤4,将其锚点设置为T,B,L和R.(设计时可以是任意大小)

步骤5,将代码复制到Form1.vb。

第6步,运行它,然后你就可以了。

第7步,如果适合你,请将此标记为5.00。我希望在3天内获得10 * 5.00.



Step 1, Create a Form called Form1.
Step 2, Add a TableLayoutPanel called TableLayoutPanel1.
Step 3, Make it 10 X 10 with 10% each.
Step 4, Set its anchor to T, B, L, and R. (It could be in any size in design time)
Step 5, Copy the code to Form1.vb.
Step 6, Run it, and you get it.
Step 7, If it works for you, please mark this one as 5.00. I wish to get 10 * 5.00 in 3 days.

Public Class Form1
    'Use 0 based array
    Private NRow As Integer = 9
    Private NCol As Integer = 9
    Private BtnArray((NRow + 1) * (NCol + 1) - 1) As Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TableLayoutPanel1.Size = Me.ClientSize
        For i As Integer = 0 To BtnArray.Length - 1
            BtnArray(i) = New Button()
            BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            BtnArray(i).Text = CStr(i)
            TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod (NCol + 1), i \ (NCol + 1))
            AddHandler BtnArray(i).Click, AddressOf ClickHandler
        Next
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As _
   System.EventArgs)
        MsgBox("I am button #" & CType(sender, Button).Text)
    End Sub
End Class