65.9K
CodeProject 正在变化。 阅读更多。
Home

电话索引

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (1投票)

2011年2月22日

CPOL

1分钟阅读

viewsIcon

25382

downloadIcon

528

这是一个电话索引程序,用于展示如何使用英文字母(A..Z)来搜索以任何字母开头的客户姓名。

引言

我之前写过一篇文章来创建PictureBoxArray,还写过另一篇文章来创建LabelTextBox控件的Array。你也可以阅读轻松创建用于英文字母 A..Z 的二十六个按钮的代码。现在,在这篇文章中,我将使用这二十六个按钮的想法来编写一个电话索引程序,以展示如何使用英文字母(A..Z)来搜索以任何字母开头的客户姓名,你还可以了解一些 ADO.NET 的方法。

背景

我创建了两个项目,一个使用 C# 编写代码,另一个使用 VB 编写代码。本文中的代码将使用 C# 编写,你可以在展开文件后阅读这两个项目。

  • 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
}

备注

解压缩文件(*.zip)后,你可以阅读关于使用 ADO 方法的剩余代码,并查看程序运行结果。

结束语

我希望这篇文章对您有所帮助。如果您有任何想法,请告诉我。感谢 CodeProject,感谢大家。

Mostafa Kaisoun
m_kaisoun@hotmail.com

历史

  • 2011 年 2 月 21 日:初始版本
© . All rights reserved.