Reputation: 8786
I have a combobox on form1 that I need to call on form2 to get the user selection. Can some one please give me an example on how to do this?
EDIT: Forgot to explain what Im trying to do. I have a readonly textbox....a user clicks edit to edit the text but I want the text they want/chose to edit to pop up right when form2 is called.
I have this code on form1
public string SelectedComboValue
{
get { return comboBox1.SelectedItem.ToString(); }
}
And this code on form 2
EDIT: Added Form1 form1 = null; BUT its still not returning the SelectedComboValue
public Form2(Form1 parentForm1) : this()
{
form1 = parentForm1;
}
But it gave me an error saying that form1 is not in this context
Upvotes: 0
Views: 5047
Reputation: 3
You can wrap the combobox an object of the ComboBox class like this:
internal static ComboBox CB=comboBox1;
Then you can call it in the other form, and access all of the methods and attributes of the ComboBox class. If you want to add items to that CB, you can do it easily as you do in the parent form. It doesn't matter if it's internal or static, it's just for the example.
Upvotes: 0
Reputation: 1
In VB it is much more automated:
Form1: textbox and button in clicking the button in form1 put the code:
Form2.Show()
in Form2: on the Load put this code:
ComboBox1.Text = Form1.TextBox1.Text
Upvotes: 0
Reputation: 1
in c# Form 2: create a combobox here
public string strDecVal{
set{ combobox1.text = value; }
}
in Form 1: for example you have a textbox and a button that will go to form2
put these code on your button
Form2 frmShow = new Form2(); //Calling the form2
frmShow.strDecVal = textbox1.text;
frmShow.ShowDialog;
Upvotes: 0
Reputation: 216253
I suppose that Form1 is the parent of Form2, so when you create the Form2 you use code like this
Form2 f = new Form2(this);
then in the Form2 class you should have a declaration like this
Form1 _parentForm = null;
and in the Form2 constructor
public Form2(Form1 parentForm1)
{
_parentForm = parentForm1;
}
If this is true then you can call
_parentForm.SelectedComboValue ;
to get the result required
Upvotes: 2