4

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.

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

I have tried:

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


        private void OnListBoxItemClick(object sender, EventArgs e)
        {
            var form2 = new House();
            House.ShowDialog();
        }
  1. This would only allow me to open one form. How can I assigned different forms to be open with different values from listbox?
  2. I would like the form to open after I click on the button, not the value in the listbox, how to achieve it?
Community
  • 1
  • 1
jaspernorth
  • 415
  • 1
  • 10
  • 28

2 Answers2

4

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;
    }

}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • I have tried it, but nothing is happening. No errors, but no results either – jaspernorth Aug 05 '13 at 19:08
  • @jaspernorth have you tried putting in breakpoints at the switch statement to see what `SelectedItem` is? – jszigeti Aug 05 '13 at 19:09
  • I have tried inserting MessageBox.Show(listBox1.SelectedItem.ToString()); at the top of button1_Click but again, nothing is happening. Do I need to initialize it somehow? – jaspernorth Aug 05 '13 at 19:11
  • 2
    @jaspernorth It sounds like you are not subscribed to the `Click` event. Go to button1 in the Designer, go to the Events tab, and set the `Click` event to use `button1_Click`. – jszigeti Aug 05 '13 at 19:15
  • 1
    @jaspernorth You need to add the click event to your button in your designer or add it to the Button in your Constructor. Try putting a breakpoint on the button1_click event to see if it is being hit. – Mark Hall Aug 05 '13 at 19:15
  • @jszigeti It worked! Thanks for your contribution and patience. I'm quite new to programming, and this silly thing would probably take me ages to solve. Thanks again – jaspernorth Aug 05 '13 at 19:18
  • @MarkHall thank you for your answer. This is precisely what I needed. Thanks again! – jaspernorth Aug 05 '13 at 19:20
0

You can use a Dictionary to map the values in your selected items to forms, or perhaps better yet functions that can create a form (to ensure lazy loading).

private Dictionary<string, Func<Form>> formMapping 
    = new Dictionary<string, Func<Form>>()
    {
        {"House", ()=> new House()},
        {"People", ()=> new People()},
        {"Outdoor", ()=> new Outdoor()},
    };

Then you can use that mapping in the click event handler to translate the selected value into a form:

private void button1_Click(object sender, EventArgs e)
{
    Form newForm = formMapping[listBox1.SelectedValue]();
    newForm.ShowDialog();
}
Servy
  • 202,030
  • 26
  • 332
  • 449