-
Notifications
You must be signed in to change notification settings - Fork 18
/
ToolStripMenuRadioItem.cs
128 lines (109 loc) · 3.66 KB
/
ToolStripMenuRadioItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace ScintillaNET_Kitchen
{
public class ToolStripMenuRadioItem : ToolStripMenuItem
{
#region Constructors
public ToolStripMenuRadioItem()
: base()
{
this.Initialize();
}
public ToolStripMenuRadioItem(Image image)
: base(image)
{
this.Initialize();
}
public ToolStripMenuRadioItem(string text)
: base(text)
{
this.Initialize();
}
public ToolStripMenuRadioItem(string text, Image image)
: base(text, image)
{
this.Initialize();
}
public ToolStripMenuRadioItem(string text, Image image, params ToolStripItem[] dropDownItems)
: base(text, image, dropDownItems)
{
this.Initialize();
}
public ToolStripMenuRadioItem(string text, Image image, EventHandler onClick)
: base(text, image, onClick)
{
this.Initialize();
}
public ToolStripMenuRadioItem(string text, Image image, EventHandler onClick, Keys shortcutKeys)
: base(text, image, onClick, shortcutKeys)
{
this.Initialize();
}
public ToolStripMenuRadioItem(string text, Image image, EventHandler onClick, string name)
: base(text, image, onClick, name)
{
this.Initialize();
}
#endregion
private void Initialize()
{
this.Click += ToolStripMenuRadioItem_Click;
}
private void ToolStripMenuRadioItem_Click(object sender, EventArgs e)
{
foreach (var item in this.FindItemsInSameGroup())
item.CheckState = CheckState.Unchecked;
this.Checked = true;
}
protected ToolStripMenuRadioItem[] FindItemsInSameGroup()
{
ToolStripItem current, next;
var parent = this.GetCurrentParent();
var result = new List<ToolStripMenuRadioItem>();
// find previous items
current = this;
while (
(next = current.GetPrevItem()) != null
&& !(next.Text == "-" || next is ToolStripSeparator)
)
{
current = next;
if (current is ToolStripMenuRadioItem) result.Add(current as ToolStripMenuRadioItem);
}
// find next items
current = this;
while (
(next = current.GetNextItem()) != null
&& !(next.Text == "-" || next is ToolStripSeparator)
)
{
current = next;
if (current is ToolStripMenuRadioItem) result.Add(current as ToolStripMenuRadioItem);
}
// return result
return result.ToArray();
}
}
public static class ToolStripItemExtensionMethods
{
public static int GetIndex(this ToolStripItem item)
{
var parent = item.GetCurrentParent();
return parent == null ? -1 : parent.Items.IndexOf(item);
}
public static ToolStripItem GetPrevItem(this ToolStripItem item)
{
var i = item.GetIndex() - 1;
return i < 0 ? null : item.GetCurrentParent().Items[i];
}
public static ToolStripItem GetNextItem(this ToolStripItem item)
{
var i = item.GetIndex() + 1;
var c = item.GetCurrentParent().Items.Count;
return i >= c ? null : item.GetCurrentParent().Items[i];
}
}
}