call an event handler

W

Wilfried Mestdagh

Hi,

I want to execute (call) an eventhandler of an object. In this case a
menu item. I want to see which item is checked and the checked one has
to execute the Click event handler, as it was clicked. But I'm a little
lost on how to call it.

ToolStripMenuItem item;
for (int i = 0; i < viewToolStripMenuItem.DropDownItems.Count; i++) {
item = (ToolStripMenuItem)viewToolStripMenuItem.DropDownItems;
if (item.Checked) {
; // Here I want to call the event handler Click from this menu

// as it was clicked.
return;
}
}
 
M

Marc Gravell

try item.PerformClick();

However, a few people will probably jump up and down and say that you
shouldn't be doing this... you should be calling the backing method
directly. You could argue that the above way is closer to the user's
actions, and ensures that *all* listeners to the event get invoked...

You can also probably same some code by just enumerating the DropDownItems
with a foreach.

Up to you...

Marc
 
W

Wilfried Mestdagh

Hi Marc,

Thank you. Yes indeed foreach is more elegant in this case thx again :)
 

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