2

実行時に 1、2、3、および 4 と呼ばれる 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

クラス終了

4

3 に答える 3

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 に答える
0

Controls.Findメソッドを使用して、フォーム 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 に答える