Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

vb.net - How to create Control Arrays in VB .NET

In VB6 there is a feature called Control Arrays, where you name controls the same name and provide them an index value. This allows you to set a value by looping through the controls and setting each value. In VB .NET I can't create a control array could someone provide me with a similar solution.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a sample I wrote for something else that shows how to do something similar and shows how to do the handler as well. This makes a 10x10 grid of buttons that turn red when you click them.

Dim IsCreated(99) As Boolean
Dim Buttons As New Dictionary(Of String, Button)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    For i As Integer = 0 To 99
        Dim B As New Button
        Me.Controls.Add(B)
        B.Height = 30
        B.Width = 40
        B.Left = (i Mod 10) * 41
        B.Top = (i  10) * 31
        B.Text = Chr((i  10) + Asc("A")) & i Mod 10 + 1
        Buttons.Add(B.Text, B)
        B.Tag = i
        AddHandler B.Click, AddressOf Button_Click
    Next


End Sub

Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim B As Button = sender
    IsCreated(B.Tag) = True
    B.BackColor = Color.Red
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...