2

For example I have Form1 and Form2. Form1 has public property

 public IMyCustomType selectedOption;

Inside Form1 there is comboBox with custom object list. I want when user select option from that combo list to selectedOption populate with that value, like

public IMyCustomType selectedOption;

private void availableChoices_SelectedIndexChanged(object sender, EventArgs e)
{             
   selectedOption = (IMyCustomType)availableChoices.SelectedItem;                                        
}

Inside Form2 I want to create new instance of that selected type. How to access from form2 to that selectedOption property and use that value to create new instance?

kirelagin
  • 13,248
  • 2
  • 42
  • 57
panjo
  • 3,467
  • 11
  • 48
  • 82
  • While @Griedius has given you a solution, got to say the design you are using is not good. You've tightly coupled the forms and separation of concerns has gone walk about. An interface and a separate class to implement the common stuff would be the way to go. – Tony Hopkinson Jun 04 '13 at 11:58
  • ok, thanks for the info, any links or materials to read? – panjo Jun 04 '13 at 12:17
  • Try this http://stackoverflow.com/questions/269496/inheritance-vs-aggregation but any good OO book should cover it. think of it this way, if you wanted to reuse form1 without form2 or vice versa, you will be in mess quick. – Tony Hopkinson Jun 04 '13 at 15:36

4 Answers4

3

if form2 is shown from form1, you may use overload with owner parameter:

form2.Show(form1);

then in form2 you simple take it from owner property:

((Form1)this.Owner).selectedOption

Or alternatively you can craete public method in form2, that would accept selected option as parameter and call it before showing form2.

public class Form2 : Form {
  private IMyCustomType parentSelectedOption;
  ...
  public void InitParameters(IMyCustomType selectedOption) 
  {
      parentSelectedOption = selectedOption;
  }
}

public class Form1 : Form {
     ....
     var form2 = new Form2();
     form2.InitParameters(selectedOption);
     form2.Show();
}

Mind naming though, public properties (in your case it is field) are usually named using CamelCase.

Giedrius
  • 8,430
  • 6
  • 50
  • 91
1

Best practice I know is to have a controller class that controls the forms of your program, you can then pass a reference to a state object to your forms on creation and access any properties you need from that state object.

(This also helps you do neat tricks like setting the next form to be loaded from within your current form; it will be very easy to pick up by your controller class to then call etc..)

Izzy
  • 1,764
  • 1
  • 17
  • 31
0

Please provide more details and I will add to my answer. It depends on how you create Form2. One simple way to accomplish this is to put IMyCustomType selectedOption in the default constructor of form2 and pass your selectedOption from Form1 to Form2 through the constructor of Form2.

CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • That selection should be available to many forms, Form1 is the main form and many other forms should use that property. – panjo Jun 04 '13 at 11:31
  • Giedrius has provided a solution above. As he stated just make sure you pass Form1 as the owner in each form you want to access the property. – CodeCamper Jun 04 '13 at 11:36
-1

The combo box properties window will have a member visibility of private. Set it to internal or public.

baxter
  • 42
  • 2