C# winforms combobox dynamic autocomplete
Here is my final solution. It works fine with a large amount of data. I use Timer
to make sure the user want find current value. It looks like complex but it doesn't.
Thanks to Max Lambertini for the idea.
private bool _canUpdate = true;
private bool _needUpdate = false;
//If text has been changed then start timer
//If the user doesn't change text while the timer runs then start search
private void combobox1_TextChanged(object sender, EventArgs e)
{
if (_needUpdate)
{
if (_canUpdate)
{
_canUpdate = false;
UpdateData();
}
else
{
RestartTimer();
}
}
}
private void UpdateData()
{
if (combobox1.Text.Length > 1)
{
List<string> searchData = Search.GetData(combobox1.Text);
HandleTextChanged(searchData);
}
}
//If an item was selected don't start new search
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
_needUpdate = false;
}
//Update data only when the user (not program) change something
private void combobox1_TextUpdate(object sender, EventArgs e)
{
_needUpdate = true;
}
//While timer is running don't start search
//timer1.Interval = 1500;
private void RestartTimer()
{
timer1.Stop();
_canUpdate = false;
timer1.Start();
}
//Update data when timer stops
private void timer1_Tick(object sender, EventArgs e)
{
_canUpdate = true;
timer1.Stop();
UpdateData();
}
//Update combobox with new data
private void HandleTextChanged(List<string> dataSource)
{
var text = combobox1.Text;
if (dataSource.Count() > 0)
{
combobox1.DataSource = dataSource;
var sText = combobox1.Items[0].ToString();
combobox1.SelectionStart = text.Length;
combobox1.SelectionLength = sText.Length - text.Length;
combobox1.DroppedDown = true;
return;
}
else
{
combobox1.DroppedDown = false;
combobox1.SelectionStart = text.Length;
}
}
This solution isn't very cool. So if someone has another solution please share it with me.
Auto Complete feature in C# win forms dynamic
You can config a TextBox
to use a custom source for auto complete:
- Set
AutoCompleteMode
property of aTextBox
toSuggest
,Append
orSuggestAppend
- Set
AutoCompleteSource
property toCustomSource
. - Assign a custom source to
AutoCompleteCustomSource
property ofTextBox
.
Example
In the below example, I set list of month names as auto complete source. You can use any string[]
as auto complete custom source.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
var source = new AutoCompleteStringCollection();
source.AddRange(System.Globalization.CultureInfo
.InvariantCulture.DateTimeFormat.MonthNames);
textBox1.AutoCompleteCustomSource = source;
How to create an Auto-complete combo-box or text box to filter text containing a string
If you mean show suggestions then it's a simple matter of changing a property when you have the TextBox selected in your IDE of choice:
The options shown in the picture allow you to change the rules for autocompleting text in the text box as well as the source for the suggestions. (Visual Studio 2010)
You can go to the following link to learn more about the TextBox control.
MSDN Text Box Control
How to use auto-complete TextBox or a ComboBox in webform
The equivalent control for a ComboBox in ASP.Net is the DropDownList. You can use JQuery for autocomplete style functionality. See this link: http://jqueryui.com/autocomplete/#combobox.
Change the auto-complete behaviour of a textbox
I assume you're asking about a winforms textbox, as I dont think the WPF textbox supports autocomplete at all.
The base TextBox
class will not support doing what you want, so you could in theory attempt to override all of the functionality in the TextBox
class to do what you want, but the better idea would be to create a new custom control that inherits from TextBoxBase
and implement the autocomplete behavior the way you want it.
change comboBox to Auto Complete Text Field codename one
You don't need a renderer since the model would return strings which is what you need for the auto complete to work.
You need to convert your code so your model is a list of strings and then just place an auto complete with that.
Related Topics
Case-Insensitive Dictionary with String Key-Type in C#
Mvvm Show New Window from Vm When Seperated Projects
Newtonsoft Inline Formatting for Subelement While Serializing
The Difference Between Virtual, Override, New and Sealed Override
When Not to Use Yield (Return)
Run the Current Application as Single Instance and Show the Previous Instance
Using C# to Search a CSV File and Pull the Value in the Column Next to It
Filter a List by Another List C#
Change Custom Attribute's Parameter at Runtime
.Net Core 3 Yields Different Floating Point Results from Version 2.2
How to Update an Existing Windows Phone 8 App to Windows Phone Store 8.1
How to Make Form1 Label.Text Change When Checkbox on Form2 Is Checked
How to Validate Signature of Jwt from Jwks Without X5C
How to Detect That C# Windows Forms Code Is Executed Within Visual Studio
How to Convert a File into Byte Array in Memory