在C#中可以使用AutoCompleteStringCollection类来实现输入框的自动完成功能。
首先需要创建一个包含所有候选项的字符串集合(List)或者从文件、数据库等地方获取到这些候选项。然后将该集合传递给TextBox控件的AutoCompleteCustomSource属性。
下面是一个示例代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AutocompleteExample
{
public partial class Form1 : Form
{
private List<string> suggestions = new List<string>(); // 存放候选项的列表
public Form1()
{
InitializeComponent();
// 添加一些候选项到suggestions列表中
suggestions.Add("Apple");
suggestions.Add("Banana");
suggestions.Add("Orange");
suggestions.Add("Grapes");
suggestions.Add("Mango");
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; // 设置为自动追加模式
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; // 设置为自定义源
textBox1.AutoCompleteCustomSource.AddRange(suggestions.ToArray()); // 设置自定义源为suggestions列表
}
}
}
对于TextEdit控件想要实现相同的功能
textEdit1.Properties.AdvancedModeOptions.AutoCompleteMode = DevExpress.XtraEditors.TextEditAutoCompleteMode.SuggestAppend;
textEdit1.Properties.AdvancedModeOptions.AutoCompleteSource = AutoCompleteSource.CustomSource;
textEdit1.Properties.UseAdvancedMode = DevExpress.Utils.DefaultBoolean.True;
textEdit1.Properties.AdvancedModeOptions.AutoCompleteCustomSource.AddRange(suggestions.ToArray());
比textbox控件多一步设置UseAdvancedMode