当我双击特定项目时,如何将form2列表框中的选定项转移到form1中的文本框 [英] how to transfer selected item in listbox in form2 to textbox in form1 when i double click on particular item
问题描述
要求尽快提供帮助 我创建了Windows appltn,其中包含两种形式,一种形式是标签,另一种是两种文本框,如label,textbox1(item_code),2nd textbox2(item_discription).当我双击该项目代码的特定项目时,项目说明进入form1文本框,以指示代码的传输方式.
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代码:
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);
}
}
}
}
推荐答案
这是实现该目标的一种方法,非常简单.在第一种形式中,当您启动Form2时,将变量设置为引用Form1
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();
在Form2中,添加前面提到的此变量:
In Form2, add this variable mentioned before:
public Form1 referenceToForm1;
更改了Form2中的列表框选择后,请在Form1中设置文本框.
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);
}
}
}
}
这篇关于当我双击特定项目时,如何将form2列表框中的选定项转移到form1中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!