【问题标题】:How to create search box and show the value in another form in c#?如何在 C# 中创建搜索框并以另一种形式显示值?
【发布时间】:2020-05-27 22:37:30
【问题描述】:

我正在用 Windows 窗体 c# 创建一个用于汽车交易的软件。我想要一个搜索框,可以在其中按 ID 搜索客户,并且该客户的所有详细信息都以另一种形式显示。 我正在使用 Visual Studio 2019 并使用本地数据库来保存详细信息。

【问题讨论】:

    标签: c#


    【解决方案1】:

    就实现搜索框而言,一种简单的方法是使用标准TextBox 和随附的Button 来搜索输入后提供的文本。

    这是一个示例,说明如何使用名为 textbox 的 TextBox 处理此输入,具体取决于您的数据库/数据结构的设置方式。

    假设您的客户数据位于 table 中,其中每组详细信息的键是一个 字符串,表示该客户的 ID 号,而存储的值是一个类的实例 详细信息,其中包含有关该客户的任何信息。即,添加到此表看起来像...

    customerDetailsTable.Add("0123456", new Details(string Name, int age, .......))
    

    代码可以使用文本框进行搜索,按钮看起来像这样:

    private void searchButton_Click(object sender, EventArgs e)  // <-- click event for button
    {
        if(textBox.Text.Length > 0) // <-- if something has been entered to search
        {
            Details customerDetails = customerDetailsTable[textBox.Text]; // search table
            if(customerDetails != null) // <-- if an entry exists for a customer of this ID   
            {
               using(DetailsPage page = new DetailsPage(customerDetails))
               {
                  page.ShowDialog();
               }
            }
            else // <-- if NO entry exists for a customer of this ID
            {
               // ALERT USER THAT NO CUSTOMER EXISTS FOR THIS ID (via MessageBox, however)
            }
        }
    }
    

    如您在上面的示例中所见,如果找到包含该客户 ID 详细信息的对象,它会通过构造函数传递到我命名为 DetailsPage 的新表单。这是您将创建的用于显示这些详细信息的表单,在表单的构造函数中采用一个 Details 参数,例如...

    public partial class DetailsPage : Form
    {
        private Details deets;
    
        public DetailsPage(Details d)
        {
            deets = d;
    
            // however you plan on displaying the details
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多