1

I have a dialog that is used to select which forms to show. Originally it was just being selected from a combo box, but now we need to select multiple, so we changed it to a list box.

Here is the method that we used for the combo box:

if (view.ShowDialog() == DialogResult.OK)
{          
    if (view.FormType == "Form1")
        return new Form1_Controller();
    else if (view.FormType == "Form2")
        return new Form2_Controller();
    else if (view.FormType == "Form3")
        return new Form3_Controller();
    else return null;
}
else
{
    return null;
}

How can we encapsulate this in a loop that will return a controller for each selection?

For example, I have tried something like

foreach (ListBoxItem listItem in view.ListBox1)
{
    //do if (view.FormType == "Form1")
}

But I don't know the right syntax to use.

Ben
  • 2,433
  • 5
  • 39
  • 69

4 Answers4

1

You can use below code :

foreach (var item in view.ListBox1.SelectedItems)
{        
    ShowForm(item.Value);
}

private void ShowForm(formName)
{
  if (view.FormType == "Form1")
    return new Form1_Controller();
  else if (view.FormType == "Form2")
    return new Form2_Controller();
  else if (view.FormType == "Form3")
    return new Form3_Controller();
  else return null;
}
Peyman
  • 3,068
  • 1
  • 18
  • 32
1

use SelectedItems:

foreach (var item view.ListBox1.SelectedItems)
    SelectForm(item.ToString());


void SelectForm(string value)
{
    if(value == "Form1")
        return new Form1_Controller();
    ...
}
dovid
  • 6,354
  • 3
  • 33
  • 73
1

To open all forms simultaneously, I would try a different approach something like this

ArrayList controllersSelected = new ArrayList();

foreach (var item in view.ListBox1.SelectedItems)
    GetSelectedItem(item.Value, out controllersSelected);

//Your logic to display selected forms simultaneously
DisplaySimultaneousForms(controllersSelected);

private void GetSelectedItem(formName, out ArrayList list)
{
  if (view.FormType == "Form1")
    list.Add(new Form1_Controller());
  else if (view.FormType == "Form2")
    list.Add(new Form2_Controller());
  else if (view.FormType == "Form3")
    list.Add(new Form3_Controller());
}
Kira
  • 1,403
  • 1
  • 17
  • 46
  • @Ben, Refer this question for displaying multiple forms simultaneously http://stackoverflow.com/questions/15300887/run-two-winform-windows-simultaneously – Kira Apr 02 '15 at 05:54
1
private void button1_Click(object sender, EventArgs e)
  {
     for (int i = 0; i <= listBox1.SelectedItems.Count - 1; i++) 
      {
           switch (listBox1 .Items [i].ToString ())
                {
                    case "FirstForm":
                        Form2 frm2 = new Form2();
                        frm2.Show();
                        break;
                    case "SecondForm":
                        Form3 frm3 = new Form3();
                        frm3.Show();
                        break;
                    default:
                        break;
               }
       }
  }
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36