VB2008 - Dynamically generating handles clause?

R

Rob W

Greetings,

I have an event that is run any time the mouse enters any of the menus and
tools strips items.

I had to pain stakingly select from a list of 30 odd items to complete the
handles clause (see code at bottom of post).
Is there a way they could be added dynamically (I am aware event handlers
can)?

Thanks
Rob

(code below)

Private Sub updateStatusLabelMenuSelection(ByVal sender As System.Object,
ByVal e As System.EventArgs) _

Handles NewCtrlNToolStripMenuItem.MouseEnter,
CopyToolStripMenuItem.MouseEnter, CutToolStripMenuItem.MouseEnter, _

DeleteToolStripMenuItem.MouseEnter, EditToolStripMenuItem.MouseEnter,
ExitAltF4ToolStripMenuItem.MouseEnter, _

HelpToolStripMenuItem.MouseEnter, NewCtrlNToolStripMenuItem.MouseEnter,
NewToolStripButton.MouseEnter, _

OpenCtrlOToolStripMenuItem.MouseEnter, OpenToolStripButton.MouseEnter,
PasteToolStripButton.MouseEnter, _

PasteToolStripMenuItem.MouseEnter, SaveAsToolStripMenuItem.MouseEnter,
SaveCtrlSToolStripMenuItem.MouseEnter, _

SaveToolStripButton.MouseEnter, SelectAllToolStripMenuItem.MouseEnter,
SetBackgroundColorToolStripMenuItem.MouseEnter, _

SetFontColorToolStripMenuItem.MouseEnter,
SetFontToolStripMenuItem.MouseEnter, TimeDateToolStripMenuItem.MouseEnter, _

UndoToolStripMenuItem.MouseEnter, WordWrapToolStripMenuItem.MouseEnter,
CopyToolStripButton.MouseEnter, _

AboutMyNotePadToolStripMenuItem.MouseEnter



'Set status text using tooltip property value by identifying the sender
object

If TypeOf sender Is ToolStripMenuItem Then

Me.ToolStripStatusLabel1.Text = DirectCast(sender,
ToolStripMenuItem).ToolTipText

ElseIf TypeOf sender Is ToolStripButton Then

Me.ToolStripStatusLabel1.Text = DirectCast(sender,
ToolStripButton).ToolTipText

End If

End Sub
 
G

Gregory Emerson

While this is an absolute pain it is probably the most maintainable.
If you dynamically add handlers to the objects someone debugging your
app would have to go back and forth between the two places.

However if you're looking for easy rather than maintainable and the
items are in an actual list of some sort you can loop over the
controls and use AddHandler rather than having the 20 line "handles"
on your method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top