2012-10-17 28 views
2

我有一個VB窗體,在運行時動態創建4個組合框稱爲1,2,3和4。問題在於,在訪問它們時,我最好的方法是進行以下操作,但當然這根本不起作用,有什麼想法?VB訪問動態創建的文本框

謝謝, 山姆。

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 

末級

回答

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 
+0

我建議通過此方法創建組合框的列表。那麼不需要類型轉換,再加上你將知道使用for循環實際處理哪個組合框。 – WozzeC

+0

我同意。更好的控制。 –

+0

感謝您的回覆!將有一個發揮與此,看看我可以適應所有,我唯一的擔心是,在運行時,可能有任何數量的下拉框,這是否仍然工作? 再次感謝! – user1752983

0

我會採取不同的方式,與AddHandler的。因爲這樣你可以避免有一個按鈕來收集值。只需要一個包含所有值的列表,並且如果更改了內容,就會更改列表中的值。 I F你還想要我在我的代碼的末尾添加按鈕

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 
0

您還可以使用Controls.Find方法找到您的組合框在窗體的ControlCollection

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