列表中的自动完成文本框

问题描述 投票:0回答:2

我有一个包含字符串的列表

List<string> custList = new List<string>();
我得到一个
textbox
我无法找到或想办法在我的文本框中书写时搜索列表。 列表字符串格式是这样的
211 | john smith | 03125468879
我需要能够按名称或电话号码搜索,我尝试了其他方法但过滤器从头开始。

c# winforms
2个回答
0
投票

要在 C# 中搜索字符串列表,可以使用 LINQ(语言集成查询)根据条件过滤列表。在这种情况下,我们要根据在文本框中输入的姓名或电话号码来过滤列表。

首先,我们需要获取在文本框中输入的文本。我们可以通过处理文本框的

TextChanged
事件来做到这一点。每次文本框中的文本更改时都会引发此事件。然后我们可以使用在文本框中输入的文本来过滤列表。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string searchText = textBox1.Text;
    List<string> filteredList = custList.Where(s => s.Contains(searchText)).ToList();
    // display the filtered list in a ListBox or DataGridView
}

在上面的代码中,我们获取了文本框中输入的文本,并将其存储在

searchText
变量中。然后,我们使用 LINQ 根据列表中的每个字符串是否包含
custList
来过滤
searchText
。 Where 方法返回一个
IEnumerable<string>
,我们使用
ToList
方法将其转换为 List<string>。最后,我们可以在 ListBox 或 DataGridView 中显示过滤后的列表。

但是,这段代码只会根据每个字符串是否包含搜索文本来过滤列表。要具体根据姓名或电话号码过滤列表,我们需要将列表中的每个字符串拆分为其组件并分别进行比较。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string searchText = textBox1.Text;
    List<string> filteredList = custList.Where(s =>
    {
        string[] parts = s.Split('|');
        string name = parts[1].Trim();
        string phone = parts[2].Trim();
        return name.Contains(searchText) || phone.Contains(searchText);
    }).ToList();
    // display the filtered list in a ListBox or DataGridView
}

在上面的代码中,我们使用 Split 方法将列表中的每个字符串拆分为其组件。然后我们修剪姓名和电话号码以删除任何前导或尾随空格。最后,我们使用 Contains 方法分别将姓名和电话号码与搜索文本进行比较。 Where 方法返回一个

IEnumerable<string>
,我们使用
ToList
方法将其转换为 List<string>。最后,我们可以在 ListBox 或 DataGridView 中显示过滤后的列表。

请注意,此代码假定列表中的每个字符串的格式为 “ID | 姓名 | 电话号码”。如果格式不同,您需要相应地调整代码。


0
投票

我在我的代码中添加了 2 行

 AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection();  
 AutoCompleteStringCollection sourcePhone = new AutoCompleteStringCollection();

然后我用计时器检查输入是否以“0”开头,因为我所有的电话号码都以 0 开头

private void autoCompleteTimer_Tick(object sender, EventArgs e)
{
    if (NameOrPhoneTb.Text.StartsWith("0"))
    {
        NameOrPhoneTb.AutoCompleteCustomSource = sourcePhone;
    }
    else
    {
        NameOrPhoneTb.AutoCompleteCustomSource = sourceName;
    }
}

当然对于我的收藏,我在我的循环中使用这两行将我的字符串添加到收藏列表

string startWithNameSt= custName + " | " + custPhone + " | " + custId;
string startWithPhoneSt = custPhone + " | " + custName + " | " + custId;
sourceName.Add(startWithPhoneSt);
sourcePhone.Add(startWithNameSt);

拆分文本

211 | john smith | 03125468879
将电话或姓名添加到我的文本框(电话)和(姓名)我使用了一种方法

 public static string GetUntilOrEmpty(string text, string stopAt = "|")
        {
            if (!string.IsNullOrWhiteSpace(text))
            {
                int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal);

                if (charLocation > 0)
                {
                    return text.Substring(0, charLocation);
                }
            }

            return string.Empty;
        }

最后是我使用的 sql 查询

string searchString = GetUntilOrEmpty(NameOrPhoneTb.Text, "|");
© www.soinside.com 2019 - 2024. All rights reserved.