我想使用组合框创建自动完整工具。
所以我只是将一些项目添加到我的组合框中。并将这些项目设置为我的组合框的来源。
在form_load
我这样做:
private void frmInvoice_Load(object sender, EventArgs e)
{
comboBox1.AutoCompleteMode=AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
但它不起作用,当我输入一个字母时,整个单词都没有出现在组合框中。为什么?
我点这个链接:http://www.c-sharpcorner.com/UploadFile/mahesh/AutoCompletion02012006113508AM/AutoCompletion.aspx
最好的问候。答案 0 :(得分:3)
由于您已声明 CustomSource 以进行自动完成,因此您应该提供该来源:
private void frmInvoice_Load(object sender, EventArgs e)
{
comboBox1.AutoCompleteMode=AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection data = new AutoCompleteStringCollection();
// Put here the auto completions' e.g.
data.Add("My String 1");
data.Add("Autocompletion 2");
data.Add("Some stuff");
comboBox1.AutoCompleteCustomSource = data;
}
答案 1 :(得分:1)
您没有上传自定义来源。
public Form1()
{
InitializeComponent();
this.comboBox1.AutoCompleteCustomSource.AddRange
(new string[] {"Raj Beniwal", "Rohit Malhotra", "Ronit Singh", "Ravi Kumar",
"Rohit Behl", "Sanjay Singh", "Shalini Singh", "Seema Malhotra", "Savi Verma",
"Karan Kappor", "Kapil Malhotra", "Vikash Nanda", "Vikram Jain", "Amit Garg",
"Atul Wadhwani", "Ashwani Pandey"
});
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
参考文献:http://www.c-sharpcorner.com/Blogs/2050/autocomplete-combobox-in-visual-C-Sharp-2010.aspx
答案 2 :(得分:0)
我所做的是使用第三方dll。这些是Telerik。我的代码如下
<telerik:RadComboBox x:Name="radComboBox" VerticalAlignment="Top" Visibility="Visible" AllowDrop="True"
ItemsSource="{Binding AvailList}" SelectedItem="{Binding SelectedComboboxItem, Mode=TwoWay}"
IsEditable="True"
telerik:TextSearch.TextPath="DisplayName" Height="17" Margin="10,34,39,0" />
这是在xaml中。它直接从ItemSource读取并执行自动完成。
答案 3 :(得分:0)
或者你可以这样做......
private void LoadStuffNames()
{
try
{
string Query = "select stuff_name from dbo.stuff";
string[] names = GetColumnData_FromDB(Query);
comboName.AutoCompleteMode = AutoCompleteMode.Suggest;
comboName.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection x = new AutoCompleteStringCollection();
if (names != null && names.Length > 0)
foreach (string s in names)
x.Add(s);
comboName.AutoCompleteCustomSource = x;
}
catch (Exception ex)
{
}
finally
{
}
}
干杯......