How to add a list box control to a WinForm window?

To add a list box control in a WinForm window, you can follow the steps below:

  1. Open Visual Studio and create a new WinForm project.
  2. Open the form designer, locate the ListBox control in the toolbox, and drag it onto the form.
  3. Resize and reposition the list box control to fit the layout of the form.
  4. You can set the properties of a list box control in the properties window, such as name, background color, font, and so on.
  5. To add options in a list box, you can achieve it with the following code:
// 添加选项
listBox1.Items.Add("Option 1");
listBox1.Items.Add("Option 2");
listBox1.Items.Add("Option 3");
  1. You can handle the selection event of items in a list box by implementing the following code.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // 处理选项选择事件
    MessageBox.Show("选中项:" + listBox1.SelectedItem.ToString());
}

By following the above steps, you can add a list box control to a WinForm window and customize and manipulate it.

Leave a Reply 0

Your email address will not be published. Required fields are marked *