C#的WinForms组合框自动完成动态 [英] C# winforms combobox dynamic autocomplete
问题描述
我的问题是类似这样的:<一href=\"http://stackoverflow.com/questions/515561/how-can-i-dynamically-change-auto-complete-entries-in-a-c-sharp-combobox-or-text\">How我可以动态改变自动完成条目在C#组合框或文本框?
但是,我仍然没有找到解决办法。
问题简述:
我有一个组合框
和大量的记录在它显示出来。当用户开始输入我想要加载与输入文本开始记录,并提供自动完成用户。
由于主题中描述上面,我不能加载它们对сomboBox_TextChanged
,因为我始终覆盖previous结果再也看不到它们。
我可以实现这个只用组合框
? (而不是文本框
或的ListBox
)
我用这个设置:
сomboBox.AutoCompleteMode= AutoCompleteMode.SuggestAppend;
сomboBox.AutoCompleteSource= AutoCompleteSource.CustomSource;
下面是我的最终解决方案。它正常工作与大量的数据。我使用定时
来确保用户想要找到当前值。它看起来像复杂的,但事实并非如此。
由于最大Lambertini 的想法。
私人布尔_canUpdate = TRUE; 私人布尔_needUpdate = FALSE; //如果文本已被改变,那么启动定时器
//如果用户不更改文本定时器运行期间再开始搜索
私人无效combobox1_TextChanged(对象发件人,EventArgs的发送)
{
如果(_needUpdate)
{
如果(_canUpdate)
{
_canUpdate = FALSE;
的UpdateData();
}
其他
{
RestartTimer();
}
}
} 私人无效的UpdateData()
{
如果(combobox1.Text.Length→1)
{
清单&LT;串GT; searchData = Search.GetData(combobox1.Text);
HandleTextChanged(searchData);
}
} //如果项目选择不启动新的搜索
私人无效combobox1_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
_needUpdate = FALSE;
} //更新数据,只有当用户(而不是程序)变化的东西
私人无效combobox1_TextUpdate(对象发件人,EventArgs的发送)
{
_needUpdate = TRUE;
} //当计时器运行时,不要启动搜索
//timer1.Interval = 1500;
私人无效RestartTimer()
{
timer1.Stop();
_canUpdate = FALSE;
timer1.Start();
} //更新数据时,计时器停止
私人无效timer1_Tick(对象发件人,EventArgs的发送)
{
_canUpdate = TRUE;
timer1.Stop();
的UpdateData();
} //更新组合框用新数据
私人无效HandleTextChanged(列表&LT;串GT;数据源)
{
VAR文本= combobox1.Text; 如果(dataSource.Count()大于0)
{
combobox1.DataSource =数据源; 变种STEXT = combobox1.Items [0]的ToString();
combobox1.SelectionStart = text.Length;
combobox1.SelectionLength = sText.Length - text.Length;
combobox1.DroppedDown = TRUE;
返回;
}
其他
{
combobox1.DroppedDown = FALSE;
combobox1.SelectionStart = text.Length;
}
}
这解决方案不是很冷静。所以,如果有人有另一种解决方案请与我分享。
My problem is similar to this one: How can I dynamically change auto complete entries in a C# combobox or textbox? But I still don't find solution.
The problem briefly:
I have an ComboBox
and a large number of records to show in it. When user starts typing I want to load records that starts with input text and offer the user for autocomplete.
As described in the topic above I can't load them on сomboBox_TextChanged
because I always overwrite the previous results and never see them.
Can I implement this using only ComboBox
? (not TextBox
or ListBox
)
I use this settings:
сomboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
сomboBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
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.
这篇关于C#的WinForms组合框自动完成动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!