1

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.

John
  • 15,990
  • 10
  • 70
  • 110
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138

4 Answers4

5

Instead of using the Add(String, Image, EventHandler) helper method to create the drop down item, make your own ToolStripMenuItem, set it to checked, and then add it to the list.

ToolStripMenuItem item = new ToolStripMenuItem("8 kHz", null, new EventHandler(mnuAudioSamplingRate_Click));
item.Checked = (samplingRate == 8000);
toolStripMenuItemAudioSampleRate.DropDownItems.Add(item);
unholysampler
  • 17,141
  • 7
  • 47
  • 64
  • since when does ToolStripItem's class have a Checked property? http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.aspx – Jean-Christophe Fortin Feb 04 '11 at 19:28
  • @Jean-Christophe Fortin: Edited to reflect that you need `ToolStripMenuItem` for checked. I didn't spend enough time on msdn to see that `ToolStripItem` was a parent class since no class names were used in the OP. – unholysampler Feb 04 '11 at 19:44
2

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)
    });
Community
  • 1
  • 1
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
0
toolStripMenuItemAudioSampleRate.DropDownItems["8 kHz"].Checked = (samplingRate == 8000)

That might do what you want. It might be a good idea to hold on to these dynamically added items in an array somewhere, so that you don't have to use this ugly syntax.

siride
  • 200,666
  • 4
  • 41
  • 62
-1

You can create an Decorator(GOF Design Pattern) http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/book/hires/pat4dfso.htm

  • At least can I know why you VoteDown ? The Adapter Pattern is made for adding responsibilities to individual objects dynamically and transparently. – Jean-Christophe Fortin Feb 04 '11 at 19:21
  • I didn't vote down but I too can see no connection between Adapter pattern and the question being asked. – Dan Abramov Feb 04 '11 at 19:25
  • `ToolStripMenuItem` is standard framework class and already has desired functionality, he just needed to set its property. – Dan Abramov Feb 04 '11 at 19:26
  • Oups.. you're right it's the Decorator pattern I want to say. Otherwise, at first @Afnan say he want to "add" the property to a object which it exactly what the Decorator(my mistake) does. I wasn't aware that ToolStripMenuItem have already the property. – Jean-Christophe Fortin Feb 04 '11 at 19:40