2

我有一个 VB 表单,它在运行时动态创建 4 个组合框,分别称为 1、2、3 和 4。问题是当涉及到访问它们时,我阅读最好的方法是执行以下操作,但当然这根本不起作用,有什么想法吗?

谢谢,山姆。

Public Class Form1
Dim x As Integer
Dim y As Integer


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    x = 4
    y = 0
    Dim MyLocationX As Integer = 25
    Dim MyLocationY As Integer = 25
    Do While y <> x
        Dim DropDownlist As New ComboBox
        DropDownlist.Name = x
        DropDownlist.Location = New Point(MyLocationX, MyLocationY)
        Me.Controls.Add(DropDownlist)
        y = y + 1
        MyLocationY = MyLocationY + 30
    Loop
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim z as Integer = 0
    Do While z <> x
    Dim z As New ComboBox
    MsgBox(z.SelectedValue)
    z++
    Loop



End Sub

结束类

4

3 回答 3

0

您还可以使用Controls.Find方法在 Forms ControlCollection 中定位您的 ComboBox

Public Class Form1
    Dim maxDropDowns, y As Integer
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        maxDropDowns = 4
        y = 0
        Dim MyLocationX As Integer = 25
        Dim MyLocationY As Integer = 25
        Do While y < maxDropDowns
            Dim DropDownlist As New ComboBox
            DropDownlist.Name = (y + 1).ToString
            DropDownlist.Location = New Point(MyLocationX, MyLocationY)
            Me.Controls.Add(DropDownlist)
            y = y + 1
            MyLocationY = MyLocationY + 30
        Loop

    End Sub


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim cntrls() As Control
        For z = 1 To maxDropDowns
            cntrls = Controls.Find(z.ToString, True)
            If cntrls.Count > 0 Then
                MsgBox(CType(cntrls(0), ComboBox).SelectedValue)
            End If
        Next
    End Sub
End Class

或者您可以使用Dictionary,您有许多不同的选项,这完全取决于您想要做什么,我的偏好是创建一个控件数组,分配公共事件处理程序并从发送者对象中提取启动事件的控件。

Public Class Form1
    Dim maxDropDowns, y As Integer
    Dim myControls As Dictionary(Of Integer, ComboBox) = New Dictionary(Of Integer, ComboBox)
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        maxDropDowns = 25
        y = 0
        Dim MyLocationX As Integer = 25
        Dim MyLocationY As Integer = 25
        Do While y < maxDropDowns
            Dim DropDownlist As New ComboBox
            DropDownlist.Name = (y + 1).ToString
            DropDownlist.Location = New Point(MyLocationX, MyLocationY)
            myControls.Add(y, DropDownlist)
            Me.Controls.Add(DropDownlist)
            MyLocationY = MyLocationY + 30
            y = y + 1
        Loop
    End Sub


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For z = 0 To maxDropDowns - 1
            MsgBox(myControls(z).SelectedValue)
        Next
    End Sub
End Class
于 2012-10-17T16:34:49.427 回答
0

比我的第一个建议更强大一点:

 Public Class Form1
    Private _comboBoxes(3) As ComboBox

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim MyLocationX As Integer = 25
        Dim MyLocationY As Integer = 25

        For i As Integer = 1 To 4
            Dim DropDownlist As New ComboBox()

            DropDownlist.Name = i.ToString()
            DropDownlist.Location = New Point(MyLocationX, MyLocationY)

            _comboBoxes(i - 1) = DropDownlist

            Me.Controls.Add(DropDownlist)

            MyLocationY = MyLocationY + 30
        Next

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For i As Integer = 1 To 4
            MessageBox.Show(_comboBoxes(i - 1).SelectedValue)
        Next
    End Sub

End Class
于 2012-10-17T11:50:03.263 回答
0

我会用 AddHandler 做不同的事情。因为这样你就可以避免有一个按钮来收集值。只需有一个包含所有值的列表,如果发生更改,请更改列表中的值。如果您仍然想要我在代码末尾添加的按钮

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
x = 4
y = 0
Dim MyLocationX As Integer = 25
Dim MyLocationY As Integer = 25
Do While y <> x
    Dim DropDownlist As New ComboBox
    DropDownlist.Name = x
    DropDownlist.Location = New Point(MyLocationX, MyLocationY)
    '------
    AddHandler DropDownlist.SelectedIndexChanged, AddressOf controlValueChanged
    '-------
    Me.Controls.Add(DropDownlist)
    y = y + 1
    MyLocationY = MyLocationY + 30
Loop
End Sub


  Private Sub controlValueChanged(sender As System.Object, e As System.EventArgs)
   'This event is fired when you change the selection in one of your comboboxes
   Dim cbo As combobox= sender 'Sender is the combobox that you change its selection
   'Do whatever you like with cbo

  End Sub

不要忘记清除自定义事件(非托管内存)

   Private Sub removeControlValueChangedEvents()
    'Call that Sub when your form is closed
       For each cbo as combobox in Me.Controls
          RemoveHandler DirectCast(cbo , ComboBOx).SelectedIndexChanged, AddressOf controlValueChanged
       Next

   End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    For each cbo as combobox in Me.Controls
        MessageBox.Show(cbo.SelectedValue)
    Next
   End Sub
于 2012-10-17T12:34:46.220 回答