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

FindString() public method

public FindString ( string s, int startIndex ) : int
s string
startIndex int
return int
		public int FindString (string s, int startIndex)
		{
			if (s == null || Items.Count == 0) 
				return -1;

			if (startIndex < -1 || startIndex >= Items.Count)
				throw new ArgumentOutOfRangeException ("startIndex");

			int i = startIndex;
			if (i == (Items.Count - 1))
				i = -1;
			do {
				i++;
				if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
					return i;
				if (i == (Items.Count - 1))
					i = -1;
			} while (i != startIndex);

			return -1;
		}

Same methods

ComboBox::FindString ( string s ) : int

Usage 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