本文整理汇总了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;
}
}
示例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;
}
示例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 ();
}
示例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;
}
示例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;
}
示例6: GetItemTexts
private static IEnumerable<string> GetItemTexts(ComboBox comboBox)
{
return (from object item in comboBox.Items
select comboBox.GetItemText(item));
}
示例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");
}
示例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;
}
}
}