Introduction
When I wrote a phone index program using VB6, I wanted to add twenty-six Buttons to my form for alphabetical English characters A..Z (one Button holds one character) to search for records in the database file of my phone index. I found it easy because the VB6 allows you to create an array of buttons or an array of any control. When I wanted to re-write the same program using C# or VB.Net I found the addition of twenty-six Buttons to the form not practical because Visual Studio .Net does not have the same capability as VB6 to create an array of controls. In this article you will find it very easy to create an array of buttons using C#; you can use this idea to create a tool for searching in a database file.
This article has two parts.
First part
Explain how to create an array of Buttons.
You can see this trial when you run ButtonArray project.
Note. When extracting the file (ButtonArray.zip) you will find two projects: ButtonArray project, and MyPhone project.
About the Code
The following procedure to create an array of buttons
private void AddButtons()
{
int xPos = 0;
int yPos = 0;
// Declare and initialize the array of buttons
Button[] btnArray = new Button[26];
for (int i = 0; i < 26; i++)
{
// Initialize each button
btnArray[i] = new Button();
}
int n = 0;
while (n < 26)
{
// Set properties of each button
btnArray[n].Tag = n + 1;
btnArray[n].Width = 24;
btnArray[n].Height = 20;
if (n == 13)
{
xPos = 0;
yPos = 20;
}
btnArray[n].Left = xPos;
btnArray[n].Top = yPos;
// Add buttons to the panel
pnlButtons.Controls.Add(btnArray[n]);
xPos += btnArray[n].Width;
// Assign text to buttons using ASCII value
btnArray[n].Text = ((char)(n + 65)).ToString();
// Handle button click event
btnArray[n].Click += new EventHandler(ClickButton);
n++;
}
// Set control properties
btnAddButton.Enabled = false;
label1.Visible = true;
}
// Event handler for button click
private void ClickButton(object sender, EventArgs e)
{
Button btn = (Button)sender;
MessageBox.Show("You clicked character [" + btn.Text + "]");
}
Second part
Explain how to use the twenty-six Buttons to write a program for a phone index to see how we can use the alphabetical English characters (A..Z) to search a customer name which begins with a character; you can also read about some methods of ADO.NET.
You can see this trial when you run MyPhone project after extracting the file (ButtonArray.zip).
About the Code
private Button[] btnArray;
// Create and set (26) buttons for English Characters:
private void CreateButtons()
{
int xPos = 0;
int yPos = 0;
// assign number of buttons = 26
btnArray = new Button[26];
// Create (26) Buttons:
for (int i = 0; i < 26; i++)
{
// Initialize each button
btnArray[i] = new Button();
}
int n = 0;
while (n < 26)
{
// Set properties of each button
btnArray[n].Tag = n + 1;
btnArray[n].Width = 24;
btnArray[n].Height = 20;
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:
panel1.Controls.Add(btnArray[n]);
xPos += btnArray[n].Width;
// Write English Characters:
btnArray[n].Text = ((char)(n + 65)).ToString();
// Event handler for button click:
btnArray[n].Click += new EventHandler(ClickButton);
n++;
}
}
// Result of the event click Button, get the text of button and find record
private void ClickButton(object sender, EventArgs e)
{
Button btn = (Button)sender;
string strFind = btn.Text + "%";
string strSql = $"SELECT * FROM Phone WHERE [Name] LIKE '{strFind}' Order by Name";
FindAnyName(strSql); // Using (DataReader) to find records
}
Remark
After extracting the file (ButtonArray.zip) you can open the MyPhone project to read the remaining code about the use of ADO methods and see the result when running the program.
I hope this article is useful; if you have any ideas, please tell me.