001.
public
class
ButtonCollections : UserControl
002.
{
003.
public
ButtonCollections()
004.
{
005.
RefreshGrid();
006.
}
007.
int
sizeX = 10;
int
sizeY = 10;
008.
int
rowCount = 2;
009.
int
ColumnCount = 2;
010.
Size _ButtonSize =
new
Size(300, 140);
011.
Color _ButtonColor = System.Drawing.Color.FromArgb(((
int
)(((
byte
)(255)))), ((
int
)(((
byte
)(224)))), ((
int
)(((
byte
)(192)))));
012.
Color _ButtonForeColor = System.Drawing.Color.Blue;
013.
public
void
RefreshGrid()
014.
{
015.
if
(rowCount <= 0 || ColumnCount <= 0)
return
;
016.
this
.Controls.Clear();
017.
int
X = 20, Y = 20;
018.
int
row = 0, column = 0;
019.
for
(
int
i = 1; i <= rowCount * ColumnCount; i++)
020.
{
021.
Button btn =
new
Button();
022.
btn.BackColor = _ButtonColor;
023.
btn.Font =
this
.Font;
024.
btn.ForeColor = _ButtonForeColor;
025.
btn.Location =
new
System.Drawing.Point(X, Y);
026.
btn.Name =
"button"
+ column +
"_"
+ row;
027.
btn.Size = _ButtonSize;
028.
btn.Text = btn.Name;
029.
btn.UseVisualStyleBackColor =
false
;
030.
btn.Click +=
new
EventHandler(buttonChoie_Click);
031.
X += _ButtonSize.Width + sizeX;
032.
this
.Controls.Add(btn);
033.
column++;
034.
if
(i % ColumnCount == 0)
035.
{
036.
Y += _ButtonSize.Height + sizeY; X = 20;
037.
column = 0;
038.
row++;
039.
}
040.
041.
}
042.
}
043.
044.
private
void
buttonChoie_Click(
object
sender, EventArgs e)
045.
{
046.
ButtonSelect = sender
as
Button;
047.
}
048.
049.
050.
#region _Property
051.
[System.ComponentModel.Browsable(
true
)]
052.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
053.
[System.ComponentModel.Category(
"TOR Setting"
)]
054.
public
Font ButtonFont
055.
{
056.
get
{
return
this
.Font; }
057.
set
058.
{
059.
this
.Font = value;
060.
RefreshGrid();
061.
}
062.
}
063.
[System.ComponentModel.Browsable(
true
)]
064.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
065.
[System.ComponentModel.Category(
"TOR Setting"
)]
066.
public
Color ButtonColor
067.
{
068.
get
{
return
_ButtonColor; }
069.
set
070.
{
071.
_ButtonColor = value;
072.
RefreshGrid();
073.
}
074.
}
075.
[System.ComponentModel.Browsable(
true
)]
076.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
077.
[System.ComponentModel.Category(
"TOR Setting"
)]
078.
public
Color ButtonForeColor
079.
{
080.
get
{
return
_ButtonForeColor; }
081.
set
082.
{
083.
_ButtonForeColor = value;
084.
RefreshGrid();
085.
}
086.
}
087.
088.
[System.ComponentModel.Browsable(
true
)]
089.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
090.
[System.ComponentModel.DefaultValue(2)]
091.
[System.ComponentModel.Category(
"TOR Setting"
)]
092.
public
int
rowCounts
093.
{
094.
get
{
return
rowCount; }
095.
set
096.
{
097.
rowCount = value;
098.
RefreshGrid();
099.
}
100.
}
101.
[System.ComponentModel.Browsable(
true
)]
102.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
103.
[System.ComponentModel.Category(
"TOR Setting"
)]
104.
public
Size ButtonSize
105.
{
106.
get
{
return
_ButtonSize; }
107.
set
108.
{
109.
_ButtonSize = value;
110.
RefreshGrid();
111.
}
112.
}
113.
[System.ComponentModel.Browsable(
true
)]
114.
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
115.
[System.ComponentModel.DefaultValue(2)]
116.
[System.ComponentModel.Category(
"TOR Setting"
)]
117.
public
int
ColumnCounts
118.
{
119.
get
{
return
ColumnCount; }
120.
set
121.
{
122.
ColumnCount = value;
123.
RefreshGrid();
124.
}
125.
}
126.
public
Button Item(
int
row,
int
column)
127.
{
128.
return
this
.Controls.Find(
"button"
+ column +
"_"
+ row,
true
).FirstOrDefault()
as
Button;
129.
}
130.
public
virtual
void
Clear()
131.
{
132.
this
.Controls.Clear();
133.
}
134.
#endregion
135.
public
Button ButtonSelect;
136.
public
event
EventHandler ButtonClick
137.
{
138.
add
139.
{
140.
for
(
int
r = 0; r < rowCount; r++)
141.
{
142.
for
(
int
c = 0; c < ColumnCount; c++)
143.
{
144.
if
(value !=
null
&& Item(r, c) !=
null
)
145.
Item(r, c).Click += value;
146.
}
147.
}
148.
}
149.
remove
150.
{
151.
for
(
int
r = 0; r < rowCount; r++)
152.
{
153.
for
(
int
c = 0; c < ColumnCount; c++)
154.
{
155.
if
(value !=
null
&& Item(r, c) !=
null
)
156.
Item(r, c).Click -= value;
157.
}
158.
}
159.
}
160.
}
161.
162.
}