Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do a procedure each time a combo box change the item selected 1

Status
Not open for further replies.

myrgir

Programmer
Feb 7, 2006
62
CA
Hi
I have many combo boxes in a form. I would like that each time any combo box item selected change, to call a procedure.
Is there any way I can create a global procedure instead to have this event:
"Private Sub CmbUnitType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbUnitType.SelectedIndexChanged
bFindNodeText("A010", 1, Me.CmbUnitType.Text)
End Sub"

for each combo box...
thanks
 
You can have an event handler handle an event from more than one control, like so:

Private Sub CmbUnitType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbUnitType.SelectedIndexChanged, [red]AnotherComboBox.SelectedIndexChanged, YetAnotherComboBox.SelectedIndexChanged[/red]
bFindNodeText("A010", 1, Me.CmbUnitType.Text)
End Sub

This event handler will execute when the SelectedIndexChanged event fires from any of the three comboboxes in the Handles list.

In the Sub you can determine which control fired the event by testing sender.Name.

On a related note, you can also make multiple event handlers that handle the smae event for the same control:

Private Sub Handler1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbUnitType.SelectedIndexChanged
'some code
End Sub

Private Sub Handler2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbUnitType.SelectedIndexChanged
'some code
End Sub

Both of these event handlers will evecute on the CmbUnitType's SelectedIndexChanged event. I haven't exactly figured out when this actually would be useful, but it is worth noting anyway.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Hi
Thanks to take time to read me and answer to my questions.

Now, I have more than 100 combo boxes in my form (I have tab control and about 7 tab pages)
Is there any way to do a procedure that englobe each combo box? because I have to do this for each combo box that I have.
The first solution can work but does it have any other way that I don't have to enter each combo name?
Thanks in advance
 
Add this code to your form and call in in Form_Load:

Private Sub AddComboboxHandlers()
Dim ctrl As Control

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then

AddHandler CType(ctrl, ComboBox).SelectedIndexChanged, AddressOf [red]ComboBox_SelectedIndexChanged[/red]
End If
Next
End Sub

This will assign the event handler to the SelectedIndexChanged event for every combobox on your form.

Note: replace the code in red with the name of your event handler.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
What do you mean by "the name of your event handler" ?

And where do I call my procedure "bFindNodeText" with my parameters?
Sorry for my ignorance...
 
Example:

Private Sub name_of_event_handler(ByVal sender As System.Object, ByVal e As System.EventArgs) handles <event_handler>

To understand: In vb6 you would write:
Private Sub Button1_Click()
End Sub

In .NET you would write it like:
Private sub Really_anything_here(Byval .... ) HANDLES Button1.Click

So, "What do you mean by "the name of your event handler" ?"
--> The bold red.


> As for the second question:
Use the code provided by jben at form load or Initialize() routine. Copy and paste the follwing. NOTE that as you will do that programmatically, remove the "handles ... ,... ,..." because it is useless.

Private Sub CmbUnitType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
bFindNodeText("A010", 1, Me.CmbUnitType.Text)
End Sub


Right?
 
+

put something descriptive like:
Private Sub Any_ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
bFindNodeText("A010", 1, CType(sender,ComboBox).Text)
End Sub


AddHandler CType(ctrl, ComboBox).SelectedIndexChanged, AddressOf Any_ComboBox_SelectedIndexChanged




As for the "CType(sender,ComboBox).Text", you need this so as to know which of the comboboxs in your form caused the event to be triggered.

Hope I helped enough
 
ok
it seems like I miss something

I have this procedure

Private Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
bFindNodeText("A010", 1, CType(sender, ComboBox).Text)
End Sub

-----------------------------

In my form load I call this

AddComboboxHandlers()

-----------------------------

here is the definition of my procedure

Private Sub AddComboboxHandlers()
Dim ctrl As Control

For Each ctrl In Me.Controls
If TypeOf (ctrl) Is ComboBox Then
AddHandler CType(ctrl, ComboBox).SelectedIndexChanged, AddressOf SelectedIndexChanged
End If
Next
End Sub

I put a break point on "Private Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)"

and when I change de selected item in one of my combo box It doesn't break where I put the break point. so it never pass by the SelectedIdexChanged procedure

What I am missing??
 
Make sure your comboboxes are declared WithEvents. To do this go to the region at the top of your form's code, right after the Class definition. If the region is collapsed, it will be labeled 'Windows Form Designer generated code' in a gray-bordered box with a '+' sign to the left. If it is already expanded it will be labeled '#Region " Windows Form Designer generated code "' with a '-' to the left. In this region you should see the definitions of all of the bojects on your form. The comboboxes need to be dimmed something like this:

Friend WithEvents ComboBox3 As System.Windows.Forms.ComboBox

The WithEvents is necessary to make the control produce events, such as SelectedIndexChanged.

Check that and let us know what you find.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Friend WithEvents CmbWaterheating As System.Windows.Forms.ComboBox
Friend WithEvents CmbUnitsDisconnects As System.Windows.Forms.ComboBox
Friend WithEvents CmbVoltage As System.Windows.Forms.ComboBox
Friend WithEvents CmbCoolantType As System.Windows.Forms.ComboBox
Friend WithEvents CmbUnitType As System.Windows.Forms.ComboBox
Friend WithEvents CmbColors As System.Windows.Forms.ComboBox
Friend WithEvents CmbStainlessSteelConstruction As System.Windows.Forms.ComboBox
Friend WithEvents CmbPipingCoating As System.Windows.Forms.ComboBox
Friend WithEvents CmbBlowerCoating As System.Windows.Forms.ComboBox
Friend WithEvents CmbDrainPanCoating As System.Windows.Forms.ComboBox
Friend WithEvents CmbCoilCoating As System.Windows.Forms.ComboBox
Friend WithEvents CmbEnclosureCoatings As System.Windows.Forms.ComboBox
Friend WithEvents CmbOptionalFiltrationPackages As System.Windows.Forms.ComboBox
Friend WithEvents CmbCompressorOutOfAirStreamWithDamper As System.Windows.Forms.ComboBox
Friend WithEvents CmbPerforatedBlowerSectionLining As System.Windows.Forms.ComboBox
Friend WithEvents CmbSingleSideAccess As System.Windows.Forms.ComboBox
Friend WithEvents CmbDoubleWallConstruction As System.Windows.Forms.ComboBox
Friend WithEvents CmbInsulation As System.Windows.Forms.ComboBox
 
if I call directly bFindNodeText in a combobox selectedIndexChange event it is working fine, but it seems like I miss something for the global event.
 
I don't know. I have tried, and I can't get this not to work on my system. Your code looks correct, and I am running pretty much the exact same code , with the exception of what is inside the SelectedIndexChanged event and the names.

Try deleting all of the event handlers you may have for your comboboxes, then double-click on one of them to create a new event handler for that combobox's SelectedIndexChanged event. Then rename this new event handler with a more generic name (not the one you used above, make it different) and use that in your AddComboboxHandlers sub.

This is a shot in the dark....


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Wait, are your comboboxes in some container other than your form, such as a group box or a panel? If so, calling AddComboboxHandlers as it is will not work. It needs to be modified to search through any containers in your form as well. Try making these changes (new code is in red):

Private Sub AddComboboxHandlers([red]ByVal Container As Control[/red])
Dim ctrl As Control

For Each ctrl In [red]Container[/red].Controls
If TypeOf (ctrl) Is ComboBox Then
AddHandler CType(ctrl, ComboBox).SelectedIndexChanged, AddressOf SelectedIndexChanged
[red]Else
If ctrl.HasChildren Then AddComboboxHandlers(ctrl)[/red]
End If
Next
End Sub

and in Form_Load:

AddComboboxHandlers([red]Me[/red])

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top