问题
Requesting to help Soon i have created windows appltn that contains two forms in form one lable and two textbox likelabel,textbox1(item_code), 2nd textbox2(item_discription). when i double click on particular item that item code , item discription goes to form1 textboxs how to transfer code.
form2 code:
private void listBox_user_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox_user.SelectedItem != null && rdr != null)
{
try
{
if (rdr.Read())
{
textBox1_code.Text = rdr.GetString(1);
textBox2_dis.Text = rdr.GetString(2);
}
}
}
}
回答1:
This is one way to achieve that, the very straightforward one. In the first form, when you initiate Form2 set the variable to refer to Form1
Form2 form2 = new Form2();
form2.referenceToForm1= this;
form2.Show();
In Form2, add this variable mentioned before:
public Form1 referenceToForm1;
When listbox selection in Form2 changed, set textboxes in Form1.
private void listBox_user_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox_user.SelectedItem != null && rdr != null)
{
try
{
if (rdr.Read())
{
referenceToForm1.textBox1_code.Text = rdr.GetString(1);
referenceToForm1.textBox2_dis.Text = rdr.GetString(2);
}
}
}
}
来源:https://stackoverflow.com/questions/20187575/how-to-transfer-selected-item-in-listbox-in-form2-to-textbox-in-form1-when-i-dou