Solved

ComboBox Array - Processing User Selections

Posted on 2013-12-26
10
660 Views
Last Modified: 2013-12-29
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.
0
Comment
Question by:lep1
  • 5
  • 4
10 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 39741343
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
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39741948
There are 2 ways built into VB.NET to handle that situation.

If the ComboBoxes are created at run time, you simply do something like the following. Create the event procedure for one for the ComboBoxes, and then manipulate the Handles clause at the end of the declaration:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged,	ComboBox3.SelectedIndexChanged ...

Open in new window

All the ComboBoxes specified in the Handles will get their SelectedIndexChanged at the same place.

This won't work however, is you add the ComboBoxes dynamically while the application is running. In such a case, the event should be linked dynamically with code. In such a case, you can create the event as said before for the ComboBoxes that are created at design time, but for those that are created while the application is running, add code like the following:
		AddHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
		AddHandler ComboBox4.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
		AddHandler ComboBox5.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged

Open in new window

These will also find their event triggered in the same method.

To use it, and differentiate between the different controls, you use code such as the following in the event method:      
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged,	ComboBox3.SelectedIndexChanged ...

		Dim index As Integer = DirectCast(sender, ComboBox).SelectedIndex	'That is your index

		Select Case True
			Case sender Is ComboBox1
				'The event was called by ComboBox1
			Case sender Is ComboBox2
				'The event was called by ComboBox2
		End Select

	End Sub

Open in new window

0
 

Author Comment

by:lep1
ID: 39742104
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

0
 
LVL 40
ID: 39742193
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.
0
 

Author Comment

by:lep1
ID: 39742266
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).
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 40
ID: 39742367
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.
0
 

Author Comment

by:lep1
ID: 39742407
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

0
 

Author Closing Comment

by:lep1
ID: 39742412
Outstanding, very helpful.
0
 
LVL 40
ID: 39742695
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.
0
 

Author Comment

by:lep1
ID: 39744975
Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now