Reputation: 83
I have a C# windows app that has two forms Form1 which is the main form and Form2. Form1 has a combobox on it and Form2 has a textbox.
I want to put the value selected in the Form1.ComboBox1 into Form2.TextBox1.
I am trying this:
Form1 Form1Object = new Form1();
string fff = Form1Object.ComboBox1.SelectedItem.ToString(); //not working
TextBox1.Text = fff;
Problem is that when I run this Form1 is reinitialized and i don't want that. (I have a splash screen that runt when the application starts so when i run my code the splashscreen starts all over again.
Is there a way to read ComboBox1 value without restarting the first form? If I try it directly it does not work, it sees the Form1 as calss instead of object.
Form1.ComboBox1.SelectedItem.ToString(); //does not work
I am also trying to add the value to the textbox when opening the second form:
Form2 form2 = new Form2();
form2.TextBox1.Text = ComboBox1.SelectedValue.ToString();
form2.Show();
This gives me the following error: "Object reference not set to an instant of an object."
EDIT: It works using this code:
Form2 form2 = new Form2();
form2.TextBox1.Text = ComboBox1.Text;
form2.Show();
Now my question still remains: If i am in Form2 can i still get the value from form1? If not, that is ok. I will post this as a solution.
Upvotes: 0
Views: 5790
Reputation: 386
i am not sure where the problem is
while starting/opening form2
like
Form2 f2 = new Form2();
f2.Show(this);
you have a reference to form1 as 'owner'
on form2 you can just put this on any event you want or on a button or whatever
Form1 f1 = Owner as Form1;
textBox1.Text = f1.comboBox1.SelectedItem.ToString();
converted to C# ...
Upvotes: 0
Reputation: 1033
While this is not the most proper answer, it is one way to solve the problem.
Form1
Add a method to get value
public string TransmitSelectedValue()
{
return ComboBox1.SelectedItem.ToString();
}
Form2
var myvalue = ((Form1)ParentForm.Controls.Find(Form1Name,true)).TransmitSelectedValue();
Upvotes: 1
Reputation: 48179
This type of question has been asked and answered many times, and in different versions.
I would suggest looking at a few of the following I have posted in the past...
This example shows two forms where second form is passed as a parameter the first form's instance. Then, from public methods exposed on the first, the second can call them to get the values. It is your discretion on if you want to allow setting from alternate source, or just allowing a get method... could be done as a property public get; protected set;
This stackoverflow search will show several links to posts I've done in the past with slightly altering versions between different forms.
FEEDBACK FROM COMMENT
There has to be something done in your FIRST form to call the second.. is it from a click button, or based on the actual combobox selection being changed. Whatever it is, the first Example I provided SHOULD be what you need. You are not having the second form call the first.
Without a full copy\paste of the first example, all you would need to really do is in the form 2 constructor is set your text as pulled from the first...
public Form2(Form1 viaParameters) : this()
{
this.textBox1.Text = viaParameters.Combobox1.SelectedItem;
}
however, I don't know how your items are defined.. dictionary, list, array, whatever.. so you may need to typecast to get the selected item via
if( viaParameters.Combobox1.SelectedIndex > -1 )
this.textBox1.Text = viaParameters.Combobox1.Items[ viaParameters.Combobox1.SelectedIndex ].WhateverStringValue;
This way, the start of form 2 from form 1 can grab the value directly.
If you expose the method from the first form via a property or method, your text value could be as simple as
this.textBox1.Text = viaParameters.YourForm1sMethodToGetStringFromCombobox();
Upvotes: 0