How set ComboBox to auto-complete?

R

Ronald S. Cook

In my Windows forms app, I want my ComboBoxes to let the user begin to type
values and it start to populate (if a match is found). But, I don't want
the user to enter a value that is not in the list.

What properties do I need to set to what to make this happen, please?

I think it the involved properties are:

DropDownStyle
AutoCompleteMode
AutoCompleteSource

.... but there are lots of different settings.

thanks
 
Z

zacks

In my Windows forms app, I want my ComboBoxes to let the user begin to type
values and it start to populate (if a match is found).  But, I don't want
the user to enter a value that is not in the list.

What properties do I need to set to what to make this happen, please?

I think it the involved properties are:

DropDownStyle
AutoCompleteMode
AutoCompleteSource

... but there are lots of different settings.

thanks

DropDownStyle = DropDownList
 
R

Ronald S. Cook

That does only the first letter.


In my Windows forms app, I want my ComboBoxes to let the user begin to
type
values and it start to populate (if a match is found). But, I don't want
the user to enter a value that is not in the list.

What properties do I need to set to what to make this happen, please?

I think it the involved properties are:

DropDownStyle
AutoCompleteMode
AutoCompleteSource

... but there are lots of different settings.

thanks

DropDownStyle = DropDownList
 
Z

zacks

That does only the first letter.

Correct. As far as I am aware, that's all .NET ComboBox does for you.
If you want true autocomplete, then you are going to have to write
some custom code in the controls TextChanged event. But I wouldn't re-
invent the wheel, do a Google search, I'm sure you will fine some code
out there that does it. I personally have never looked for a .NET
version, but we use a VB6 version where I work.
 
R

RobinS

Here's an example. This loads a list of fonts and set up autocomplete for
them. I think one of my primary sources for figuring this out was Brian
Noyes's data binding book.

//This indicates that the combobox for the font families is being loaded.
// This is checked by the selectedIndexChanged event, which is fired
// (I think for each entry) when the databinding is added.
private bool m_Loading = false;
private DataTable dtFonts; //table of fonts
//collection to use for auto-completion of the font names
private AutoCompleteStringCollection m_Fonts;

private void LoadFontComboBox()
{
//This has to be set to true when it does the databinding, or it mucks
up
// the fontName property of the class.
m_Loading = true;
//Set up datatable to bind to the combobox so you can use autocomplete.
dtFonts = new DataTable();
dtFonts.Columns.Add(new DataColumn("fontName"));
m_Fonts = new AutoCompleteStringCollection();
InstalledFontCollection m_allFonts = new InstalledFontCollection();
foreach (FontFamily oneFamily in m_allFonts.Families)
{
if (oneFamily.IsStyleAvailable(FontStyle.Regular))
{
dtFonts.Rows.Add(oneFamily.Name);
m_Fonts.Add(oneFamily.Name);
}
}
FontFamilyComboBox.AutoCompleteCustomSource = m_Fonts;
FontFamilyComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
FontFamilyComboBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
//databind the combobox
FontFamilyComboBox.DataSource = dtFonts;
FontFamilyComboBox.DisplayMember = "fontName";
FontFamilyComboBox.ValueMember = "fontName";

m_Loading = false;
}



RobinS.
GoldMail, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top