介绍 我曾经试用过使用VB6编写电话索引程序。我想添加26个按钮到我的表格字母英语字符A..Z(一个按钮保存一个字符)在我的手机索引数据库文件中搜索记录。 我发现它很容易,因为VB6允许创建按钮数组或任何控件数组。当我想用c#或VB重写相同的程序时。我发现在窗体中添加26个按钮是不切实际的,因为Visual Studio .NET不像VB6那样能够创建控件数组。 在本文中,您将发现创建一个按钮数组非常容易,您可以使用这个思想来创建一个用于在数据库文件中搜索的工具。 稍后我将写另一篇文章来解释如何使用这个想法在作为数据库的电话索引中进行搜索。 背景 我创建了两个项目。我用c#(2003)编写了一个代码,用VB编写了另一个代码。净(2003)。你可以在本文开头的下载链接中找到我的两个项目。 使用的代码 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 Kaisounm_kaisoun@hotmail.com 本文转载于:http://www.diyabc.com/frontweb/news462.html