简体   繁体   中英

combobox Autocomplete doesn't work

I want to create a auto complete tools using combobox .

So i just add some items to my combobox .And set these items as a source of my combobox.

In form_load i do this:

private void frmInvoice_Load(object sender, EventArgs e)      
{
    comboBox1.AutoCompleteMode=AutoCompleteMode.Append;
    comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
}

But it doesn't work and when i type a letter the whole word doesn't appear in combobox.Why ?

i follow this link : http://www.c-sharpcorner.com/UploadFile/mahesh/AutoCompletion02012006113508AM/AutoCompletion.aspx

best regards.

Since you've declared CustomSource for auto completion, you should provide that source:

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;
}

You didn't upload your CustomSource.

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;
}

Refference from : http://www.c-sharpcorner.com/Blogs/2050/autocomplete-combobox-in-visual-C-Sharp-2010.aspx

What I have done is use a 3rd party dlls. These are of Telerik. My code is as follows

<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" />

This is in xaml. It directly reads from the ItemSource and does the autocompletion.

Or you can do this...

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
    {
    }

}

Cheers..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM