问题
I am making this simple Windows forms app in Visual studio in c#. I have two forms. On form1 I have a textbox,listbox and two buttons (one to insert into listbox from textbox and another to open form2). On form2 I only have a textbox. I just simply want, when click on a button (for opening form2) on form1, form2 to open and textbox to contain (on formLoad) selected item from listbox from form1. But when I click on button it says "Object reference not set to an instance of an object". What am I doing wrong? I am pretty sure it's something simple but I just can't get it.
Thanks in advance!
Here is my code:
on 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;
}
回答1:
Because in the Form2_Load
event you always create a new instance of Form1
and then access the Transfer
property which accesses listBox1.SelectedItem
which is not set for the newly created form.
You should rather pass a referece to form 1 in the button event:
on 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;
}
}
回答2:
In your Form2_Load method, you're creating a new instance of the object Form1, separate from your existing item.
Instead, you need to either: a) Pass a reference to your current Form1 object to Form2, so that Form2 can access the Transfer property. or b) Add a new property to Form2 (called Transfer, say), and then when you create Form2, assign the current textbox value to this property, like so:
Form2 f2 = new Form2();
f2.Transfer = listBox1.SelectedItem.ToString();
f2.ShowDialog();
You could also do this by adding a parameter to the constructor of Form2, although that's really a design decision.
回答3:
because you haven't selected your listbox item,value listBox1.SelectedItem is null.Practice doing try catch block
回答4:
You are creating new Form1
instance here, which is not related to Form1 instance which you used to open 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 Form1 instance does not have selected item and you have error. I suggest you to pass selected item value to Form2 when you opening Form2:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(Transfer); // pass selected item value to constructor
f2.ShowDialog();
}
All you need is changing Form2
constructor to accept this string:
public Form2(string transfer)
{
InitializeComponent();
textBox1.Text = transfer;
}
来源:https://stackoverflow.com/questions/19449048/pass-listbox-item-to-a-textbox-on-another-form-c-sharp