Combo Box Dropped Down (On Enter or GotFocus)

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
I have an event handler that's working right now, but I'd like to make it cleaner, if at all possible.

Currently it reads like this:

Private Sub ComboBox_GotFocus(ByVal sender as object,ByVal e as System.EventArgs) Handles cboBox1.GotFocus, cboBox2.GotFocus (etc...)
sender.DroppedDown = True
End Sub

I've got about 50 combo boxes that are being handled, making this a large chunk of code.
==================================================
Is there ANY way to do something like this:

Private Sub ComboBox_GotFocus((ByVal sender as object,ByVal e as System.EventArgs) Handles (??) (All?)
If TypeOf sender is ComboBox Then
sender.DroppedDown = True
End If
=================================================

I wish there already was a property for each combobox where it dropped down it's values when entered into or focus was gotten. Get on the Ball Microsoft!!!
 
In form load event handler you can iterate each control in Controls collection of a Form/Panel/Groupbox or some container, check if it is a ComboBox control and AddHandler to attach event handler to the same method. If the ComboBoxes is placed in different containers you have to use the Control.GetNextControl method to iterate all controls.

(you could also list them in the Handler statement, comma-separated list, but that would be rather tedious with many controls)
 
I have already done what you suggested in parenthesis, but as you mentioned, that's what I'm trying to avoid...very large indeed.

One last item, could you possibly show me this code that adds handler? Don't worry about the GetNextControl, I think I"ve got that covered.

I already use a load event that disables all controls if a control is marked True, and it looks at container controls as well (child controls - groupboxes,tabpages,etc...)
 
Comboboxes in click event

I'd like to revisit this.

Can you elaborate on how I could get all comboboxes to do something when the user clicks them?

I'm in the dark on the whole addressof thing, so can you start from scratch and tell me exactly what I'd need to do?

Thanks for your help.
 
Huh? There's nothing more to it than the example code line I posted above. After addressof you specify the name of the method to handle the event. The combobox50 could be any reference to a control. For example you iterating GetNextControl and "c" is the next control you use "c.theevent".
 
Dummy's level

Ok, I'm at a dummies level, sorry...

What I want to do is avoid typing all 50 comboboxes like this:

Public Sub Comboboxes_Click(ByVal sender as...etc) Handles cbo1.click,cbo2.click, etc...

I want to try some code elsewhere that says...for ALL my comboboxes cbo1 thru cbo50, execute some code when the user clicks on one of these comboboxes.
 
That's exactly why you use AddHandler, basically:
VB.NET:
For each c as control in me.controls
  addhandler c.click, addressof someclickhandler
next
 
Back
Top