![](/img/trans.png)
[英]How would I create a find/search button on form2 to search and highlight text in a rich text box on form1 in c#
[英]How to create search box and show the value in another form in c#?
我正在為 windows 從 c# 創建一個汽車交易軟件。 我想要一個搜索框,可以在其中按 ID 搜索客戶,並且該客戶的所有詳細信息都以另一種形式顯示。 我正在使用 Visual Studio 2019 並使用本地數據庫來保存詳細信息。
就實現搜索框而言,一種簡單的方法是使用帶有隨附按鈕的標准文本框來搜索輸入后提供的文本。
這是一個示例,說明如何使用名為textbox的 TextBox 處理此輸入,具體取決於您的數據庫/數據結構的設置方式。
假設您將客戶的數據放在一個表中,其中每組詳細信息的鍵是表示該客戶 ID 號的字符串,存儲的值是 class詳細信息的實例,其中包含有關該客戶的任何信息。 即,添加到這個表看起來像......
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 詳細信息的 object,它將通過構造函數傳遞到我命名為 DetailsPage 的新表單。 這是您將創建的用於顯示這些詳細信息的表單,在表單的構造函數中采用一個Details參數,例如...
public partial class DetailsPage : Form
{
private Details deets;
public DetailsPage(Details d)
{
deets = d;
// however you plan on displaying the details
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.