Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Phone Index

2.00/5 (1 vote)
21 Feb 2011CPOL1 min read 24.6K   509  
This is a program for phone index to see how we can use the alphabetical English characters (A..Z) to search a customer name which begin with any character.

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

C#
// 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++;
    }
}
C#
// 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)