Link to home
Start Free TrialLog in
Avatar of lep1
lep1

asked on

ComboBox Array - Processing User Selections

I need to setup a variable number of comboboxes for users, depending on how many comboboxes a user wants.   (BTW, each combobox has the same options).   However, if I programattically add 15 comboboxes to the control collection of a form, I will be stuck with placing all the selectedIndex processing code in each of the comboboxe's selectedindexchanged events.

Given this situation, how can I use a single selectedIndexchanged event for a combobox array, and also detect which array element and selectedindex were clicked?

My thought is that there will obviously be one selectedindexchanged event for the combobox array, so the trick is to interrogate which array element of the combobox was selected along with detecting the selectedindex.
Avatar of Ivo Stoykov
Ivo Stoykov
Flag of Bulgaria image

Hi

First once set your combos become part of the flow and they cannot be manipulated as an array.

as for "... which array element and selectedindex were clicked..." you need to attach to each of the combos one and the same function to onchange or whichever event you decide. If you use client side script attach to "onchange=function myfinc(this)". and define myfunc as
function myfunc(el) { ... } // el is the element which index has been changed

Open in new window

myfinc will get the element which selectedindex were clicked as a parameter.

If you use code behind you have again to attach one and the same method to the event and you have the same object as a sender parameter so you could decide which object has fired the event.

Hope I've described it clear enough ...

HTH

Ivo Stoykov
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lep1
lep1

ASKER

Hi James,

Without using your suggested "Combobox1" naming convention I was going to add Comboboxes to the form's control selection in a loop, and then use the name to pick off controls.  So how would I invoke the Addhandler in this instance?
  For j = 1 To ncol
            Dim str As String = "CB_" & j
            Dim CB As New ComboBox
            CB.Name = str
            CB.Items.Add("Item 1")
            CB.Items.Add("Item 2")
            CB.Items.Add("Item 3")
            CB.Width = 70
            CB.Left = 40
            CB.Top = 50 + 30 * j
            CB.SelectedIndex = 0
            Me.Controls.Add(CB)
            '-->not working: AddHandler CB.SelectedIndexChanged, AddressOf CB_1_SelectedIndexChanged
  Next
    

Open in new window

When you add a Control to a form, you need to add it to the Controls collection of the form. This is a recommended thing to do no matter if you add an event handler or not. It is required to use handles to create the delegates that are used to call the procedure.

Just before calling AddHandler, add the following:

Me.Controls.Add(CB)

Me assumes that the code is running from inside form itself. If not, replace it by the variable that references your form.
Avatar of lep1

ASKER

James, see line 12 of my listing above -- > I am adding the control to the form.  The only thing needed is how to apply the add handler you recommended).
Ooops. Missed line 12

What do you mean by not working on line 13?

Did you create a procedure called CB_1_SelectedIndexChanged that has the same parameter types as what you get when you create the event procedure the same way?

I will be out for a few hours, so my next answer could take some time.
Avatar of lep1

ASKER

The following fully solved the issue.  (ncol is the number of Comboboxes the user requests)


        For j = 1 To ncol
            Dim str As String = "CB_" & j
            Dim CB As New ComboBox
            CB.Name = str
            CB.Items.Add("Item 1")
            CB.Items.Add("Item 2")
            CB.Items.Add("Item 3")
            CB.Width = 70
            CB.Left = 40
            CB.Top = 50 + 30 * j
            CB.SelectedIndex = 0
            Me.Controls.Add(CB)
            AddHandler CB.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged
        Next
       
    Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim index As Integer = DirectCast(sender, ComboBox).SelectedIndex   'That is your index
        Dim buff() As String
        buff = Split(sender.name, "_")
        Dim cbselected As Integer = buff(1) 'This is the ComboBox that was selected

        '...Follow here with processing code for selected index of the selected ComboBox

    End Sub

Open in new window

Avatar of lep1

ASKER

Outstanding, very helpful.
Note that the trick works with anything that has events.

There is also a RemoveHandler command (same syntax) that can deactivate the event if needed. That can be useful if you need to Refresh the content of a ComboBox or a DataGridView, because doing so usually generates a lot of useless events that slows down the process.
Avatar of lep1

ASKER

Thanks.