ToolStripMenuItem.CheckOnClick Does Not Fire

  • Thread starter Thread starter rob
  • Start date Start date
R

rob

I have a menu item that has a check mark. When ever the check mark is
toggled I do a bunch of changes in the appropriate event handler. Now
when the app starts I read the last settings and set the menu
accordingly. So in my MainFrame_Load function I call:

showItToolStripMenuItem.Checked = lastShowItStatus;

Unfortunately, setting the checked state programmatically does not
trigger my event hander. Therefore, the internal state of the program
does not match the state shown in the menu. What is the best approach
to make the event handler execute when I set the checked state?

Thanks
 
You have a number of options. You can explicitly invoke the event
handler. You can extract the code from the handler into a separate
method that both the click event and the constructor call. You can
duplicate the code in the constructor.

If you have the CheckOnClick property set to true, your best bet would
be to handle the CheckChanged event and ignore the Click event
altogether, since CheckChanged is raised regardless of whether the
Checked property was changed by a user or through code.
 
I was thinking about extracting the code in a separate function as well
as calling the event handler directly but did not like either of it. I
did not think about CheckOnClick though. I tried it and it works great.
Thanks a lot for that advice.

On more question regarding the event handler. First of all my feeling
is that calling an event handler is a bad thing to do. If it is not
what exactly are the parameters to pass in. Sender I guess would be the
actual menu item in question. What about the arguments?
 
Back
Top