i got a list that contains strings List<string> custList = new List<string>();
and i got a textbox
i can't find or figure a way to search the list while writing in my textbox.
the list strings format is like this 211 | john smith | 03125468879
i need to be able to search by name or by phone number, i tried other methods but the filter begins from the beginning.
Asked
Active
Viewed 61 times
0

John Kugelman
- 349,597
- 67
- 533
- 578

محمد السروجي
- 1
- 4
-
You cannot use the inbuilt auto-complete functionality for that. You will have to handle the `TextChanged` event and search the list yourself using an appropriate comparison. If you try to implement that and it doesn't work, we'd need you to explain the logic you're trying to implement, show us the implementation and explain what actually happens. – jmcilhinney May 13 '23 at 03:05
-
what is type of your project c# .NET MVC? C# Winform? OR what? Where is your code so far? – toha May 13 '23 at 04:58
-
Relevant: [Customize textbox autocomplete](https://stackoverflow.com/questions/43254621/customize-textbox-autocomplete) – John Wu May 13 '23 at 05:31
-
@jmcilhinney i thought of an easy way to fix my problem according to my inputs, i posted my answer, thanks for your help – محمد السروجي May 14 '23 at 10:57
-
@toha c# winfrom, i thought of a fix to my problem according to my inputs, thanks for your help <3 – محمد السروجي May 14 '23 at 10:58
-
@JohnWu thank you, i thought of an easy way and posted my answer. – محمد السروجي May 14 '23 at 10:58
1 Answers
0
i added 2 lines to my code
AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection();
AutoCompleteStringCollection sourcePhone = new AutoCompleteStringCollection();
then i used a timer to check if the input start with "0" since all my phone numbers will start with 0
private void autoCompleteTimer_Tick(object sender, EventArgs e)
{
if (NameOrPhoneTb.Text.StartsWith("0"))
{
NameOrPhoneTb.AutoCompleteCustomSource = sourcePhone;
}
else
{
NameOrPhoneTb.AutoCompleteCustomSource = sourceName;
}
}
and ofcourse for my collections i used these two lines in my loop to add my strings to collection list
string startWithNameSt= custName + " | " + custPhone + " | " + custId;
string startWithPhoneSt = custPhone + " | " + custName + " | " + custId;
sourceName.Add(startWithPhoneSt);
sourcePhone.Add(startWithNameSt);
to split the text 211 | john smith | 03125468879
to get phone or name to my textbox (Phone) and (Name) i used a method
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;
}
and last for my sql query i used
string searchString = GetUntilOrEmpty(NameOrPhoneTb.Text, "|");

محمد السروجي
- 1
- 4