本文整理汇总了C#中System.Windows.Forms.ComboBox.SelectedItem属性的典型用法代码示例。如果您正苦于以下问题:C# ComboBox.SelectedItem属性的具体用法?C# ComboBox.SelectedItem怎么用?C# ComboBox.SelectedItem使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.ComboBox
的用法示例。
在下文中一共展示了ComboBox.SelectedItem属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: showSelectedButton_Click
private void showSelectedButton_Click(object sender, System.EventArgs e) {
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
"Index: " + selectedIndex.ToString());
}
示例2: Button
//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
public class Select : Form {
private Button draw = new Button();
private ComboBox color = new ComboBox();
public Select( ) {
draw.Text = "Draw";
color.Text = "Choose a color";
Size = new Size(500,250);
int w = 20;
draw.Location = new Point(20,30);
color.Location = new Point(w += 10 + color.Width, 30);
color.Items.Add("Red");
color.Items.Add("Green");
color.Items.Add("Blue");
Controls.Add(draw);
Controls.Add(color);
draw.Click += new EventHandler(Draw_Click);
}
protected void Draw_Click(Object sender, EventArgs e) {
if (color.SelectedItem.ToString() == "Red" )
Console.WriteLine("It is red.");
else if (color.SelectedItem.ToString() == "Green")
Console.WriteLine("It is green.");
else
Console.WriteLine("It is blue.");
}
static void Main() {
Application.Run(new Select());
}
}