C#中的ComboBox控件详细使用方法

  using System;

  using System.Windows.Forms;

  namespace ComboBoxExample

  {

  public partial class MainForm : Form

  {

  public MainForm()

  {

  InitializeComponent();

  }

  private void MainForm_Load(object sender, EventArgs e)

  {

  // 初始化 ComboBox 的选项

  comboBox.Items.Add("选项1");

  comboBox.Items.Add("选项2");

  comboBox.Items.Add("选项3");

  // 设置默认选中项

  comboBox.SelectedIndex = 0;

  }

  private void comboBox_SelectedIndexChanged(object sender, EventArgs e)

  {

  // 当选中项发生改变时触发的事件

  string selectedOption = comboBox.SelectedItem.ToString();

  MessageBox.Show("选中的选项是:" + selectedOption);

  }

  private void btnGetSelected_Click(object sender, EventArgs e)

  {

  // 获取当前选中项

  string selectedOption = comboBox.SelectedItem.ToString();

  MessageBox.Show("当前选中的选项是:" + selectedOption);

  }

  private void btnAddOption_Click(object sender, EventArgs e)

  {

  // 添加新选项

  string newOption = textBoxNewOption.Text;

  comboBox.Items.Add(newOption);

  MessageBox.Show("已添加新选项:" + newOption);

  }

  }

  }