how to transfer selected item in listbox in form2 to textbox in form1 when i double click on particular item

人走茶凉 提交于 2019-12-13 06:26:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!