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.
.NET ProgrammingMicrosoft DevelopmentVisual Basic.NET

Avatar of undefined
Last Comment
lep1

8/22/2022 - Mon
Ivo Stoykov

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
Jacques Bourgeois (James Burger)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
lep1

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

Jacques Bourgeois (James Burger)

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
lep1

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).
Jacques Bourgeois (James Burger)

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.
ASKER
lep1

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
lep1

Outstanding, very helpful.
Jacques Bourgeois (James Burger)

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.
ASKER
lep1

Thanks.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes