System.Windows.Forms.ComboBox.FindString C# (CSharp) Method

FindString() public method

public FindString ( string s ) : int
s string
return int
		public int FindString (string s)
		{
			return FindString (s, -1);
		}

Same methods

ComboBox::FindString ( string s, int startIndex ) : int

Usage Example

Example #1
0
        private void AutoComplete_KeyUp(ComboBox comboBox2, KeyEventArgs e)
        {
            string sTypedText = null;
            int iFoundIndex = 0;
            object oFoundItem = null;
            string sFoundText = null;
            string sAppendText = null;

            //Allow select keys without Autocompleting
            switch (e.KeyCode)
            {
                case Keys.Back:
                case Keys.Left:
                case Keys.Right:
                case Keys.Up:
                case Keys.Delete:
                case Keys.Down:
                case Keys.ShiftKey:
                case Keys.Shift:
                case Keys.RShiftKey:
                case Keys.LShiftKey:
                case Keys.Oem1:
                case Keys.Oem102:
                case Keys.Oem2:
                case Keys.Oem3:
                case Keys.Oem4:
                case Keys.Oem5:
                case Keys.Oem6:
                case Keys.Oem7:
                case Keys.Oem8:
                    return;
            }

            //Get the Typed Text and Find it in the list
            sTypedText = comboBox2.Text;
            iFoundIndex = comboBox2.FindString(sTypedText);

            //If we found the Typed Text in the list then Autocomplete

            if (iFoundIndex >= 0)
            {
                //Get the Item from the list (Return Type depends if Datasource was bound or List Created)
                oFoundItem = comboBox2.Items[iFoundIndex];

                //Use the ListControl.GetItemText to resolve the Name in case the Combo was Data bound
                sFoundText = comboBox2.GetItemText(oFoundItem);

                //Append then found text to the typed text to preserve case
                sAppendText = sFoundText.Substring(sTypedText.Length);
                comboBox2.Text = sTypedText + sAppendText;

                //Select the Appended Text
                comboBox2.SelectionStart = sTypedText.Length;
                comboBox2.SelectionLength = sAppendText.Length;

                label10.Text = ((Person)oFoundItem).Name;
                label11.Text = ((Person)oFoundItem).Email;
                label12.Text = ((Person)oFoundItem).SSN;
            }
        }
All Usage Examples Of System.Windows.Forms.ComboBox::FindString