Phone Index
Introduction
I wrote an article earlier to create Array
of PictureBox
and wrote another article to create Array
of Label
and TextBox
controls. You can also read the easy code to create Array
of twenty-six buttons for alphabetical English characters A..Z. Now, in this article, I shall use the twenty-six buttons idea to write a program for phone index to see how we can use the alphabetical English characters (A..Z) to search a customer name which begins with any character, also you can read about some methods of ADO.NET.
Background
I create two projects, I write code of one under C# and write another under VB. The code in this article will be under C#, you can read my two projects after you expand the files:
- MyPhone_C#.zip
- MyPhone_VB.zip
Using the Code
// Create and set (26) buttons for English Characters:
private void CreateButtons()
{
int xPos = 0;
int yPos = 0;
// assign number of buttons = 26
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:
panel1.Controls.Add(btnArray[n]); // Let panel hold the Buttons
xPos = xPos + btnArray[n].Width; // Left of next button
// Write English Characters:
btnArray[n].Text = ((char)(n + 65)).ToString();
// the Event of click Button:
btnArray[n].Click += new System.EventHandler(ClickButton);
n++;
}
}
// Result of the event click Button, get the text of button and find record:
private void ClickButton(Object sender, System.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
}
Remarks
After you extract the files (*.zip), you can read the remaining code about using ADO methods and see the result when running the program.
Final Words
I hope this article is useful. If you have any ideas, please tell me. Thanks to CodeProject and thanks to all.
Mostafa Kaisoun
m_kaisoun@hotmail.com
History
- 21st February, 2011: Initial version
Post Comment
gpkmfI Hey, thanks for the blog.Really thank you! Great.
qS4VyL I cannot thank you enough for the blog.Really thank you! Much obliged.
kGp55Y A big thank you for your article.Thanks Again. Really Great.