How can i see a combo in al window forms in c#
Hy. I want to use the combo selected item from form1 in form 2 and 开发者_C百科i don't know how in c#.
Please help!
- When opening Form2, pass the object containing Form1 as a reference.
- In Form2, access the combo box using this object either
- directly (note that this might require changing the access modifier of the combo box from
protected
topublic
) or - (more elegantly) by calling a public method of Form1 written by you that returns the selected item of the combo box.
- directly (note that this might require changing the access modifier of the combo box from
Not a good idea to pass a reference in form 2's constructor. Suppose the following code is in Form1.cs.
Form2 f = new Form2();
f.Tag = myCombo;
f.showDialog();
You can operate the combo in Form2 by getting the tag and parse it to ComboBox. A better way:
Form2 f = new Form2();
Dictionary<string,object> controlsInForm1 = new Dictionary<string,object>();
controlsInForm1.Add("combo",myCombo);
controlsInForm1.Add("label",myLabel);
f.Tag = controlsInForm1;
f.showDialog();
Now you can operate controls in Form1 safely without complex constructor of Form2.
pass a reference to the combo box in form 1 to form 2, possible in form 2's constructor.
I'd create a read only public property which returns the selected item.
public object ComboValue
{
get
{
return combo.SelectedItem;
}
}
In stead of object, use the proper class/type.
精彩评论