Pass list item to textbox on another form c #
I am making this simple Windows Forms Application in Visual studio in C #. I have two forms. In form1, I have a textbox, a list box, and two buttons (one for inserting into a list box from the textbox and the other for opening form2). On form2, I only have a textbox. I just want when you click on a button (to open form2) on form1, form2 to open and a textbox to contain (on formLoad) the selected item from the list from form1. But when I click the button, it says "Object reference not set on object instance". What am I doing wrong? I'm sure this is something simple, but I just can't get it.
Thanks in advance!
Here is my code:
in form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
public string Transfer
{
get { return listBox1.SelectedItem.ToString(); }
}
and on form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
textBox1.Text = f1.Transfer;
}
source to share
Because in an event, Form2_Load
you always create a new instance Form1
and then access the property Transfer
that is being accessed listBox1.SelectedItem
, which is not set for the newly created form.
You should rather pass the referee to form 1 in the button event:
in form1:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
and on form2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.Transfer;
}
}
source to share
In your Form2_Load method, you create a new instance of the Form1 object, separate from the existing item.
Instead, you need to: a) Pass a reference to your current Form1 object in Form2 so that Form2 can access the Transfer property. or b) Add a new property to Form2 (like Transfer, say) and then when you create Form2, assign the current textbox value to that property, like this:
Form2 f2 = new Form2();
f2.Transfer = listBox1.SelectedItem.ToString();
f2.ShowDialog();
You can also do this by adding a parameter to Form2's constructor, although this is indeed a design decision.
source to share
This creates a new instance Form1
that is not associated with the Form1 instance you opened Form2
:
private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1(); // here is new instance of Form1 created
textBox1.Text = f1.Transfer;
}
So this new instance of Form1 has no item selected and you have an error. I suggest you pass the selected value of the Form2 element when you open Form 2:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(Transfer); // pass selected item value to constructor
f2.ShowDialog();
}
All you need to do is change the constructor Form2
to accept this line:
public Form2(string transfer)
{
InitializeComponent();
textBox1.Text = transfer;
}
source to share