Only permitting items in the list

(YOU HAVE TO REMEMBER TO DO THIS TO STOP USERS ENTERING THEIR OWN VALUES!!!)

DropDownStyle = DropDownList (Only accepts strings that are part of the selection list)

Loading the contents of a comboBox at application startup

	cmbMyComboBox.BeginUpdate();
	cmbMyComboBox.SelectedIndex = -1;
	cmbMyComboBox.Items.Clear();		//Remove Existing Items If Necessary

	for (Count = 0; Count <= 500; Count++)	//Add Items
		cmbMyComboBox.Items.Add((Count * 100) + "mm");

	cmbMyComboBox.SelectedIndex = 0;	//Select default item
	cmbMyComboBox.EndUpdate();

	//while (cmbMyComboBox.Items.Count)	//Another method to remove items
	//	cmbMyComboBox.Items.RemoveAt(0);

Allowing User To Type Text To Find An Entry

DropDownStyle = DropDown
AutoCompleteMode = Append (or an alternative if preffered)
AutoCompleteSource = List Items
In this mode the user can enter text that doesn’t match an entry in the list. You need to detect this yourself. Unfortunately you can’t use the ‘Leave’ event of the combobox as if a user types the start of a name then uses tab to select it the SelectedIndex value is still -1 on the Leave call and only changes afterwards. You need to do the check using some other means.

	if (cmbUsername.SelectedIndex < 0)
		cmbUsername.Text = "";

Select An Item In the List

	cmbMyComboBox.SelectedIndex = 0;

Getting Currently Selected Item Text

	MyString = cmbMyComboBox.SelectedItem.ToString()
//or
	MyString = cmbMyComboBox.Items[cmbMyComboBox.SelectedIndex].ToString();
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *