选择列表框上的项目后打开新表单 [英] Open new form after selecting item on listbox

查看:19
本文介绍了选择列表框上的项目后打开新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上有一个列表框和一个按钮.Listbox 包含 3 个元素:House、People、Outdoor.我还创建了 3 个表单来表示列表框中的值.

I have a listbox and a button on my form. Listbox contains of 3 elements: House, People, Outdoor. I have also created 3 forms to represent the values from the listbox.

我希望用户突出显示列表框上的项目,并在单击按钮后打开用户选择的表单.

I would like to user to highlight the item on the listbox and after clicking the button I would like to open the form selected by the user.

我怎样才能做到这一点?我试过这个链接:通过点击一个ListBox 上的项目,但没有任何成功.

How can I achieve this? I have tried this link: Calling new Form by clicking an item on the ListBox but without any success.

我试过了:

public Select()
        {
            InitializeComponent();
            listBox1.Click += OnListBoxItemClick;
        }


        private void OnListBoxItemClick(object sender, EventArgs e)
        {
            var form2 = new House();
            House.ShowDialog();
        }

  1. 这只会让我打开一个表单.如何分配不同的表单以从列表框中使用不同的值打开?
  2. 我希望点击按钮后打开表单,而不是列表框中的值,如何实现?

推荐答案

您将需要使用 ListBox 的 SelectedItem 属性:

You will need to use the ListBox's SelectedItem Property:

private void button1_Click(object sender, EventArgs e)
{
    switch (listBox1.SelectedItem.ToString())
    {
        case "House":
            House h = new House();
            h.ShowDialog();
            break;
        case "People":
            People p = new People();
            p.ShowDialog();
            break;
        case "Outdoor":
            Outdoor o = new Outdoor();
            o.ShowDialog();
            break;
    }

}

这篇关于选择列表框上的项目后打开新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆