在C#中,要在组合框或文本框中动态更改自动完成条目,可以使用AutoCompleteStringCollection
类和AutoCompleteMode
属性。以下是一个简单的示例:
using System.Windows.Forms;
AutoCompleteStringCollection
对象,并将其添加到组合框或文本框的AutoCompleteCustomSource
属性中:AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();
autoCompleteCollection.AddRange(new string[] { "选项1", "选项2", "选项3" });
// 对于组合框
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.AutoCompleteCustomSource = autoCompleteCollection;
// 对于文本框
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = autoCompleteCollection;
autoCompleteCollection
对象并将其重新分配给AutoCompleteCustomSource
属性即可:autoCompleteCollection.Add("新选项");
// 对于组合框
comboBox1.AutoCompleteCustomSource = autoCompleteCollection;
// 对于文本框
textBox1.AutoCompleteCustomSource = autoCompleteCollection;
这样,您就可以在C#中的组合框或文本框中动态更改自动完成条目了。
领取专属 10元无门槛券
手把手带您无忧上云