當前位置: 首頁>>代碼示例>>C#>>正文


C# ComboBox.GetItemText方法代碼示例

本文整理匯總了C#中System.Windows.Forms.ComboBox.GetItemText方法的典型用法代碼示例。如果您正苦於以下問題:C# ComboBox.GetItemText方法的具體用法?C# ComboBox.GetItemText怎麽用?C# ComboBox.GetItemText使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.ComboBox的用法示例。


在下文中一共展示了ComboBox.GetItemText方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AutoComplete_KeyUp

        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;
            }
        }
開發者ID:stefaneyd,項目名稱:combobox-autocomplete,代碼行數:60,代碼來源:Form1.cs

示例2: CalculateComboBoxDropdownWidth

        // Calculates maximum required width of the dropdown portion for the ComboBox.
        // This is done in order not to cut off the longest dropdown item.
        public static int CalculateComboBoxDropdownWidth(ComboBox comboBox)
        {
            if (comboBox == null)
                return SystemInformation.VerticalScrollBarWidth;

            int scrollbarOffset = 0;

            if (comboBox.Items.Count > comboBox.MaxDropDownItems)
            {
                scrollbarOffset = SystemInformation.VerticalScrollBarWidth;
                try
                {
                    scrollbarOffset += SystemInformation.HorizontalFocusThickness; // padding
                }
                catch (NotSupportedException)
                {
                    scrollbarOffset += 2; // 2 pixels padding
                }
            }

            float maxWidth = 0.0f;
            using (System.Drawing.Graphics ds = comboBox.CreateGraphics())
            {
                foreach (object item in comboBox.Items)
                {
                    maxWidth = Math.Max(maxWidth, ds.MeasureString(comboBox.GetItemText(item), comboBox.Font).Width);
                }
            }

            int newWidth = (int)decimal.Round((decimal)maxWidth, 0) + scrollbarOffset;

            //If the width is bigger than the screen, ensure
            //we stay within the bounds of the screen
            if (newWidth > Screen.GetWorkingArea(comboBox).Width)
            {
                newWidth = Screen.GetWorkingArea(comboBox).Width;
            }

            // Don't let dropdown area to be outside of the screen
            if (comboBox.Parent != null)
            {
                int screenWidth = Screen.FromControl(comboBox).Bounds.Width;
                Point pt = comboBox.Parent.PointToScreen(new Point(comboBox.Left + newWidth, comboBox.Bottom));
                if (pt.X > screenWidth)
                    newWidth = Math.Max(comboBox.Bounds.Width, newWidth - (pt.X - screenWidth));
            }

            // Only change the width if the calculated width is larger that the current width
            return comboBox.Bounds.Width > newWidth ? comboBox.Bounds.Width : newWidth;
        }
開發者ID:CuriousX,項目名稱:annotation-and-image-markup,代碼行數:52,代碼來源:AimWinFormsUtil.cs

示例3: DrawComboBoxItem

		public override void DrawComboBoxItem (ComboBox ctrl, DrawItemEventArgs e)
		{
			Color back_color, fore_color;
			Rectangle text_draw = e.Bounds;
			StringFormat string_format = new StringFormat ();
			string_format.FormatFlags = StringFormatFlags.LineLimit;
			
			if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
				back_color = ColorHighlight;
				fore_color = ColorHighlightText;
			}
			else {
				back_color = e.BackColor;
				fore_color = e.ForeColor;
			}
			
			if (!ctrl.Enabled)
				fore_color = ColorInactiveCaptionText;
							
			e.Graphics.FillRectangle (ResPool.GetSolidBrush (back_color), e.Bounds);

			if (e.Index != -1) {
				e.Graphics.DrawString (ctrl.GetItemText (ctrl.Items[e.Index]), e.Font,
					ResPool.GetSolidBrush (fore_color),
					text_draw, string_format);
			}
			
			if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
				CPDrawFocusRectangle (e.Graphics, e.Bounds, fore_color, back_color);
			}

			string_format.Dispose ();
		}
開發者ID:hindlemail,項目名稱:mono,代碼行數:33,代碼來源:ThemeWin32Classic.cs

示例4: SetComboBoxDropDownWidth

 public static void SetComboBoxDropDownWidth(ComboBox cbx)
 {
     //if (!cbx.IsHandleCreated || !(cbx.DataSource is DataTable)) { return; }
     //bool isDatabound = cbx.DataSource != null && cbx.DisplayMember != null && cbx.DisplayMember != "";
     int width = cbx.DropDownWidth,
         vScrollbarWidth = cbx.Items.Count > cbx.MaxDropDownItems ? SystemInformation.VerticalScrollBarWidth : 0;
     using (System.Drawing.Graphics g = cbx.CreateGraphics())
     {
         for (int i = 0; i < cbx.Items.Count; i++)
         {
             width = Math.Max((int)g.MeasureString(cbx.GetItemText(cbx.Items[i]), cbx.Font).Width + vScrollbarWidth, width);
         }
     }
     cbx.DropDownWidth = width;
 }
開發者ID:SawyerNursery,項目名稱:PottingInformation,代碼行數:15,代碼來源:Global4k.cs

示例5: SetComboBoxDropDownWidth

 /// <summary>
 /// 根據下拉框內容設置下拉列表顯示寬度
 /// </summary>
 /// <param name="cbx">要進行設置的下拉框組件</param>
 private void SetComboBoxDropDownWidth(ComboBox cbx)
 {
     int dropDownWidth = 0;
     foreach (object item in cbx.Items)
     {
         dropDownWidth = Math.Max(dropDownWidth,
             TextRenderer.MeasureText(cbx.GetItemText(item), cbx.Font).Width);
     }
     cbx.DropDownWidth = dropDownWidth;
 }
開發者ID:MuteG,項目名稱:gpmagic,代碼行數:14,代碼來源:FormCardInfo.cs

示例6: GetItemTexts

 private static IEnumerable<string> GetItemTexts(ComboBox comboBox)
 {
     return (from object item in comboBox.Items
             select comboBox.GetItemText(item));
 }
開發者ID:ckittel,項目名稱:HoiPolloi,代碼行數:5,代碼來源:ComboBoxExtensions.cs

示例7: FormatEventValueType

		public void FormatEventValueType ()
		{
		       	string event_log = null;
			ComboBox comboBox = new ComboBox ();
			comboBox.FormattingEnabled = true;
			comboBox.Format += delegate(object sender, ListControlConvertEventArgs e)
			{
				event_log = e.Value.GetType ().Name;
			};
			
			int [] objects = new int [] { 1, 2, 3 };
			comboBox.DataSource = objects;
			comboBox.GetItemText (1);

			Assert.AreEqual (typeof (int).Name, event_log, "#A0");
		}
開發者ID:nlhepler,項目名稱:mono,代碼行數:16,代碼來源:ListControlTest.cs

示例8: SelectInCombo

 private void SelectInCombo(ComboBox combo, string text)
 {
     for (int i = 0; i < combo.Items.Count; i++)
     {
         if (text == combo.GetItemText(combo.Items[i]))
         {
             combo.SelectedIndex = i;
             break;
         }
     }
 }
開發者ID:gmilazzoitag,項目名稱:OpenLiveWriter,代碼行數:11,代碼來源:WatermarkEditor.cs


注:本文中的System.Windows.Forms.ComboBox.GetItemText方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。