简体   繁体   English

如何将日期从列表框发送到另一个表单的文本框

[英]How to send date from a listbox to textbox of another form

I have a listbox with multiple items in a form. 我有一个表单中包含多个项目的列表框。 I need to select listbox item and click a button and then selected item should appear in another form's textbox. 我需要选择列表框项并单击一个按钮,然后所选项应出现在另一个表单的文本框中。 How can I do this? 我怎样才能做到这一点?

I use this code to put items to my form 1 list box after I click a button. 单击按钮后,我使用此代码将项目放入表单1列表框中。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();                
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn2 = new SqlConnection(
            "<path>\\Database1.mdf\";Integrated Security=True");
        conn2.Open();

        ArrayList al = new ArrayList();
        SqlCommand commandtwo = new SqlCommand(
            "SELECT name FROM [dbo].[Table2]", conn2);
        SqlDataReader dr2 = commandtwo.ExecuteReader();

        while (dr2.Read())
        {
            al.Add(dr2[0].ToString());
        } 

        try
        {
            listBox1.Items.Clear();
            listBox1.Items.AddRange(al.ToArray());
            conn2.Close();
        }
        catch (Exception) 
        {}
    }

    public void button2_Click(object sender, EventArgs e)
    {         
        Form2 f2 = new Form2();
        f2.Show();
    }
}

I need to select item in this listbox and click Update button in this form, it will open another form called Form2. 我需要在此列表框中选择项目并单击此表单中的“更新”按钮,它将打开另一个名为Form2的表单。 I need to get selected item from previous form (Form1) and show it in a textbox in another form (Form2). 我需要从以前的表单(Form1)中获取所选项目,并在另一个表单(Form2)的文本框中显示它。

This is the code of my second form (Form2) 这是我的第二个表格的代码(Form2)

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        textBox1.Text = f1.listBox1.SelectedItem.ToString();
    }
}

There's many ways to do it. 有很多方法可以做到这一点。 One is to have public property on first form that you access from the other form. 一种是在第一个表单上拥有您从另一个表单访问的公共属性。 But as said, this is only one approach (you could use events, pass value in overloaded constructor, etc.) 但正如所说的,这只是一种方法(你可以使用事件,在重载的构造函数中传递值等)

// Form1
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    SelectedItem = (sender as ComboBox).SelectedItem.ToString();
}

private void button_Click(object sender, EventArgs e)
{
    var frm2 = new Form2() { Owner = this };
    frm2.Show();
}

public string SelectedItem { get; private set; }

And then... 然后...

// Form2
protected override void OnActivated(EventArgs e)
{
    base.OnActivated(e);
    textBox1.Text = (Owner as Form1).SelectedItem;
}

You have to pass the value to the Form2 in the constructor. 您必须将值传递给构造函数中的Form2。

public Form2(string value)
{
     InitializeComponent();
     textBox1.Text = value;
}

In your Form1 just call it like this: 在你的Form1中,只需将其称为:

// get the item from listbox into variable value for example
Form2 = new Form2(value);
f2.Show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM