Search code examples
c#toolstripchecked

Add checked = true via programming


I have been adding items to tool strip by programming but the issue is that I need to add checked property to it. Do not know how to do so. Here is the code:

toolStripMenuItemAudioSampleRate.DropDownItems.Add("8 kHz", null, new EventHandler(mnuAudioSamplingRate_Click));
toolStripMenuItemAudioSampleRate.Checked = (samplingRate == 8000);//Checks if the there is no vid device

Now I know that it will work wrong because I have added checked property to toolStripMenuItemAudioSampleRate not the 8 kHz. I am trying to add this property to the dynamically added items.

Thanks in advance.


Solution

  • To make this code fancier, I suggest removing new EventHandler, which is always redundant, and using object initializer:

    toolStripMenuItemAudioSampleRate.DropDownItems.Add (
        new ToolStripMenuItem ("8 kHz", null, mnuAudioSamplingRate_Click) {
            Checked = (samplingRate == 8000)
        });