BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 


Monday, September 17, 2007
A ToolStripMenuItem and a Check

One nice addition to the .NET 2.0 framework is the ToolStripMenuItem Class. The ToolStripMenuItem Class replaces the MenuItem Class. The ToolStripMenuItem has a Checked property that gets or sets a value indicating if the item is checked or not. Coupled with the ToolStripMenuItem.CheckOnClick property, this can be useful for identifying which item has been selected or activated. Unfortunately, there isn't some sort of 'allow only one checked' property which allows for only one item in a menu to be 'Checked' at a time. This functionality can be easily added in code (or you could derive your own class that has this functionality; a nice addition to a personal control library). Create an OnClick and OnCheckChanged method that are referenced by each of the ToolStripMenuItems. Basically, you check to see if the current item is checked or not and then toggle the check for the other menu items. You could also check another property of the sender (such as the Tag property) to perform some other action.

Here is an example:

1 private void CheckedIconsToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
2 {
3 if (sender is ToolStripMenuItem)
4 {
5 if (!((ToolStripMenuItem)sender).Checked) return;
6 foreach (ToolStripMenuItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
7 {
8 if (item != null && item != sender && item.Checked)
9 {
10 item.Checked = false;
11 return;
12 }

13 }

14 }

15 }

16
17 private void CheckedToolStripMenuItem_Click(object sender, EventArgs e)
18 {
19 if (sender is System.Windows.Forms.ToolStripMenuItem)
20 {
21 if (!((ToolStripMenuItem)sender).Checked)
22 {
23 ((ToolStripMenuItem)sender).Checked = !((ToolStripMenuItem)sender).Checked;
24 }
;
25 switch (((ToolStripMenuItem)sender).Tag.ToString())
26 {
27 case "1":
28 break;
29 case "2":
30 break;
31 case "3":
32 break;
33 case "4":
34 break;
35 case "5":
36 break;
37 default:
38 break;
39 }
;
40 }

41 }

42

Labels: , ,

posted by Brad Prendergast at 9:10:00 PM
Comments:
Links to this post:

Create a Link

Recent Posts
 Who is copying your web site?
 A little FileIconInit for the System Image list
 Code Monkey
 A DataGridView, DataGridViewCheckBoxColumn, DataGr...
 Those Commercials
 System Information and the PerformanceCounter Clas...
 Cryptography hash and a class - to go
 C# LocalAdapterInformation
 PInvoke WIN32 and .NET
 ASP.NET upload a file HtmlInputFile Control


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger