Same Controls for one Event Handler

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
Does anyone know how I can make sure all controls of the same type (for example (all combo boxes OR all text boxes with the tag, "Same") are handled at a particular event.

For example: if I want ALL combo boxes to do something when the user clicks on them, rather than name every single combo box in my Click Event Handler, how can I tell the program that if it's a combobox it needs to be part of this particular event function.

Ex:

Public Sub combobox_click (ByVal sender...etc) Handles (here is where I want to eliminate having to list every "combobox".click)
 
You have the AddHandler statement and GetNextControl iteration to dynamically attach some event for controls, as per this thread.
Another way is inheriting a Combobox control and adding the inherent behaviour to it, then use this control instead of the default MS one.
VB.NET:
class herCombox
inherits Combobox
' add default class code
end class
 
Inheriting

So, if I create this special combobox class, how would the comboboxes that I've added to my form know to belong to this class?

And what exactly am I putting in this class again...assuming that my click event was this:

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

sender.droppeddown = True

End Sub
 
Currently I drag and drop the combobox (standard MS one) onto my interface and Microsoft creates all that behind-the-scenes stuff to add it to my form.

Should I basically replace part of this code with my own named new cbo class?

And If I do, I'm still not sure what I'm supposed to add to the new cbo class so that it does the click event code.

Sorry, I'm still learning OOP concepts.
 
You use a inherited class the same way as the default class, either drag it from toolbox to form, or create instance in code with New keyword and add it to a Controls collection.
still not sure what I'm supposed to add to the new cbo class so that it does the click event code.
If you want the click code to display a messagebox you write msgbox("hi") in either click event of that class or better practice in OnClick method. Example:
VB.NET:
Public Class herCombox
    Inherits ComboBox

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        MyBase.OnClick(e)
        MsgBox("hi")
    End Sub
End Class
Now all herCombox control instances in form will say "hi" when you click them :D
 
This works great, thanks.

I've just learned another OOP concept.
I'm sure it's a basic one, but we all operate on different levels.

Thanks so much!
 
Back
Top