1

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.

Alex78191
  • 2,383
  • 2
  • 17
  • 24
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • 1
    Since you've declared CustomSource for autocompletion, you should provide that source: `comboBox1.AutoCompleteCustomSource = data;` – Dmitry Bychenko Jun 25 '14 at 09:59
  • [Visit Here](http://stackoverflow.com/questions/11780558/c-sharp-winforms-combobox-dynamic-autocomplete) for the same – Aman Jun 25 '14 at 10:01

4 Answers4

4

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;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • thanks ,but one question ,how can i prevent to add my items one by one to my AutoCompleteStringCollection .i get my data from database as a string list collection,how can i set my colelction as a datasource for AutoCompleteStringCollection ? – Ehsan Akbar Jun 25 '14 at 10:11
  • Suppose my 'var q=myUserRepository.getall().tolist()? – Ehsan Akbar Jun 25 '14 at 10:17
  • 1
    Look at `AddRange()` http://msdn.microsoft.com/en-us/library/system.windows.forms.autocompletestringcollection.addrange(v=vs.110).aspx something like data.AddRange(myUserRepository.Select(record => record.SomeStringField()).ToArray()) – Dmitry Bychenko Jun 25 '14 at 10:26
1

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

user3770158
  • 379
  • 1
  • 5
  • 16
0

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.

Jay Nirgudkar
  • 426
  • 4
  • 18
0

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..

Jay Nirgudkar
  • 426
  • 4
  • 18