COmboBox array control

techmanbd

Junior Contributor
Joined
Sep 10, 2003
Messages
397
Location
Burbank, CA
I made a combobox control array

Code:
dim cboLoad(26) as controlbox

for x as integer = 1 to intChannels
     sboLoad(x) = new controlbox
     With cboLoad(x)
                .Size = New Size(32, 16)
                .Visible = True
                .DropDownStyle = ComboBoxStyle.DropDownList
                .Location = New Point(8, intXPoint)
                For y As Integer = 0 To 6 Step 2
                    .Items.Add(y)
                Next
       End With

I tried to make a "SelectedIndexChanged" sub for my array and I can't. It says that the control needs to be WithEvents. Now you can't use WithEvents for an array.

Is there a way so if I select something in the combobox list I can have something happen like the SelectedIndexChanged.
 
Add an index property

Can you stub the SelectedIndexChanged() from a combobox, then add a handler to it for each combobox in the array on Initialize?
 
rustyd said:
Can you stub the SelectedIndexChanged() from a combobox, then add a handler to it for each combobox in the array on Initialize?

That is exactly what you want to do, but in case you don't know how, look up AddHandler in the help. Also, you won't be able to retrieve the index in the handler as you could in VB6. What I usually do, though, if I need the index, is store the index in the control's .Tag property as a string. You can then use the following code to retrieve the tag.

Visual Basic:
Dim Index As Integer = CInt(DirectCast(Sender, Control).Tag)
 
In the selectedIndexChanged() you could use the sender instead of an index
With

Code:
Dim s as string = DirectCast(sender, ComboBox).Name

Select Case s
    Case "ComboBox1"
    Case "ComboBox2"
    case Else
end select
 
OK I am a little lost. I haven't heard the term stub. before. What do you mean by this.

here was what I have after trying to read the help files.

Code:
Event cboLoadHandle(ByVal cbo As Integer)

Public Sub ddddddd()
        RaiseEvent cboLoadHandle(Me.cboLoad(1).SelectedItem)
        Dim str As String
        MessageBox.Show(str)
End Sub

Public Sub nnnnn()
     AddHandler cboLoadHandle, AddressOf cboloadhandlehandler
     MessageBox.Show("addhandler")
End Sub

Private Sub cboloadhandlehandler(ByVal cbo As Integer)
        MessageBox.Show("handled")
End Sub

am I at least on the right track, or way off
 
rustyd said:
In the selectedIndexChanged() you could use the sender instead of an index
With

Code:
Dim s as string = DirectCast(sender, ComboBox).Name

Select Case s
    Case "ComboBox1"
    Case "ComboBox2"
    case Else
end select
First of all, if you want to do this you need to set the program up for it. The code posted did not set the .Name property of the controls. I don't know if the default name is an empty string or "ComboBox(insert number here)", but even in the case of the latter, there is no guaruntee that the numbers will begin at one (for example, if there is already a combo box named "ComboBox1"). You will need to set the names explicitly. Alternatively, if you object to my method, I would recommend using something like "If sender Is cboLoad(i) Then..." (this requires that you keep a reference to each combobox).

Secondly, the "stub" for a combo box SelectedIndexChanged event would be:
Visual Basic:
Sub Pick_A_Name(ByVal sender As System.Object, ByVal e As System.EventArgs)
'You code goes here.
'This is also where you determine which combobox is clicked (if you need to)
End Sub

To get the signature (stub, delegate, whatever) for any specific event, you can search for the control's class in the object browser, look for the event you want to handle, select it, and look at the definition in the bottom of the window. Alternatively, you can add a control to your form, go to the code editor, select the control in the list of objects in the left combo box at the top of the code window, select the event you want to handle in the right combo box, and VB will insert the handler for you, which you can copy and paste (changing the name and removing the "Handles" clause).

Here is an example that will do what you want:

Visual Basic:
    Dim cboLoad(26) As ComboBox
    Sub MakeMeComboBoxesPlease()
        For x As Integer = 1 To intChannels
            cboLoad(x) = New ComboBox
            With cboLoad(x)
                .Size = New Size(32, 16)
                .Visible = True
                .DropDownStyle = ComboBoxStyle.DropDownList
                .Location = New Point(8, intXPoint)
                For y As Integer = 0 To 6 Step 2
                    .Items.Add(y)
                Next

                .Tag = x.ToString 'Do this if you want to store the index as a tag
                .Name = "cboLoad" & x.ToString 'Do this if you want to do a select case using the names
                AddHandler cboLoad(x).SelectedIndexChanged, AddressOf Pick_A_Name
            End With
        Next
    End Sub

    Sub Pick_A_Name(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'You code goes here.
        'This is also where you determine which combobox is clicked (if you need to ) 
    End Sub
 
Back
Top