简单的代码来创建按钮数组






4.22/5 (16投票s)
只需几行代码即可创建二十六个用于英文字母(A..Z)的按钮
引言
我之前尝试用 VB6 编写一个电话索引程序。我希望在我的表单上添加二十六个按钮,用于英文字母 A..Z(每个按钮对应一个字母),以便在电话索引的数据库文件中搜索记录。
我发现这很容易,因为 VB6 允许你创建按钮数组或任何控件的数组。
当我想要使用 C# 或 VB.NET 重写相同的程序时,我发现将二十六个按钮添加到表单中不切实际,因为 Visual Studio .NET 没有 VB6 那样的创建控件数组的可能性。
在本文中,你将发现创建按钮数组非常容易,你可以使用这个想法来创建一个用于在数据库文件中搜索的工具。
我稍后会写另一篇文章来解释如何使用这个想法在电话索引数据库中进行搜索。
背景
我创建了两个项目。我用 C# (2003) 编写了一个代码,并用 VB.NET (2003) 编写了另一个。你可以在本文顶部的下载链接中找到这两个项目。
Using the Code
C# 代码
private void AddButtons()
{
int xPos = 0;
int yPos = 0;
// Declare and assign number of buttons = 26
System.Windows.Forms.Button[] btnArray = new System.Windows.Forms.Button[26];
// Create (26) Buttons:
for (int i = 0; i < 26; i++)
{
// Initialize one variable
btnArray[i] = new System.Windows.Forms.Button();
}
int n = 0;
while(n < 26)
{
btnArray[n].Tag = n + 1; // Tag of button
btnArray[n].Width = 24; // Width of button
btnArray[n].Height = 20; // Height of button
if(n == 13) // Location of second line of buttons:
{
xPos = 0;
yPos = 20;
}
// Location of button:
btnArray[n].Left = xPos;
btnArray[n].Top = yPos;
// Add buttons to a Panel:
pnlButtons.Controls.Add(btnArray[n]); // Let panel hold the Buttons
xPos = xPos + btnArray[n].Width; // Left of next button
// Write English Character:
btnArray[n].Text = ((char)(n + 65)).ToString();
/* ****************************************************************
You can use following code instead previous line
char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'}; btnArray[n].Text = Alphabet[n].ToString();
//**************************************************************** */
// the Event of click Button
btnArray[n].Click += new System.EventHandler(ClickButton);
n++;
}
btnAddButton.Enabled = false; // not need now to this button now
label1.Visible = true;
}
// Result of (Click Button) event, get the text of button
public void ClickButton(Object sender, System.EventArgs e)
{
Button btn = (Button) sender;
MessageBox.Show("You clicked character [" + btn.Text + "]");
}
VB 代码
Private Sub AddButtons()
Dim xPos As Integer = 0
Dim yPos As Integer = 0
Dim n As Integer = 0
' Declare and Initialize one variable
Dim btnArray(26) As System.Windows.Forms.Button
For i As Integer = 0 To 25
btnArray(i) = New System.Windows.Forms.Button
Next i
While (n < 26)
With (btnArray(n))
.Tag = n + 1 ' Tag of button
.Width = 24 ' Width of button
.Height = 20 ' Height of button
If (n = 13) Then ' Location of second line of buttons:
xPos = 0
yPos = 20
End If ' Location of button: .Left = xPos
.Top = yPos
' Add buttons to a Panel:
pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
xPos = xPos + .Width ' Left of next button
' Write English Character: .Text = Chr(n + 65)
' ****************************************************************
' You can use following code instead previous line
' Dim Alphabet() As Char = {"A", "B", "C", "D", "E", "F", "G", _
' "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", _
' "U", "V", "W", "X", "Y", "Z"}
' .Text = Alphabet(n)
' ****************************************************************
' for Event of click Button
AddHandler .Click, AddressOf Me.ClickButton
n += 1
End With
End While
btnAddButton.Enabled = False ' not need now to this button now
label1.Visible = True
End Sub
' Result of (Click Button) event, get the text of button
Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs) _
ndles MyBase.Click
Dim btn As Button = sender
MessageBox.Show("You clicked character [" + btn.Text + "]")
End Sub
结束语
我希望这篇文章对您有所帮助。如果您有任何想法,请告诉我。感谢 CodeProject 以及所有人的支持。
Mostafa Kaisoun
m_kaisoun@hotmail.com