如何从隐藏表单到当前表单获取文本框数据?

时间:2014-06-10 14:02:39

标签: c# winforms

在我的应用程序中我有多个表单form1到form6,我在表单之间通过hide和show方法切换,在表单3我有多个文本框和复选框,我需要获取这些文本框数据和复选框状态按钮单击事件上的form6文本框...

public partial class Form6 : Form
{

     Form3 _form3;
     Form4 _form4;
    public Form6()
    {
        InitializeComponent();
    }

    private void L_Click(object sender, EventArgs e)
    {

            _form3 = new Form3();

            string str1 = _form3.textBox1.Text;
            textBox21.Text = (str1 + Environment.NewLine).ToString();
            string str2 = _form3.textBox11.Text;
            textBox21.Text = (str2 + Environment.NewLine).ToString();

            int result1 = _form3.checkBox1.CheckState == CheckState.Checked ? 1 : 0;
            int result2 = _form3.checkBox11.CheckState == CheckState.Checked ? 1 : 0;
            int result3 = _form3.checkBox21.CheckState == CheckState.Checked ? 1 : 0;
            int result4 = _form3.checkBox31.CheckState == CheckState.Checked ? 1 : 0;
            int result5 = _form3.checkBox41.CheckState == CheckState.Checked ? 1 : 0;
            int result6 = _form3.checkBox51.CheckState == CheckState.Checked ? 1 : 0;
            int result7 = _form3.checkBox61.CheckState == CheckState.Checked ? 1 : 0;
            int result8 = _form3.checkBox71.CheckState == CheckState.Checked ? 1 : 0;
            int result9 = _form3.checkBox81.CheckState == CheckState.Checked ? 1 : 0;
            int result10 = _form3.checkBox91.CheckState == CheckState.Checked ? 1 : 0;

            textBox21.Text = (result1).ToString();
            textBox21.Text = (result2).ToString();
            textBox21.Text = (result3).ToString();
            textBox21.Text = (result4).ToString();
            textBox21.Text = (result5).ToString();
            textBox21.Text = (result6).ToString();
            textBox21.Text = (result7).ToString();
            textBox21.Text = (result8).ToString();
            textBox21.Text = (result9).ToString();
            textBox21.Text = (result10).ToString();
}

2 个答案:

答案 0 :(得分:1)

您可以查看本文,它向您展示了如何将数据从一个winform传递到另一个winform的不同选项 http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx

答案 1 :(得分:0)

您的问题是您没有引用Form3的当前实例,而不是创建一个新实例,请尝试:

    Form3 form3;
    public Form6(Form3 form3)
    {
        InitializeComponent();
        this.form3=form3;
    }


 private void L_Click(object sender, EventArgs e)
    {
       int result1 = form3.checkBox1.CheckState == CheckState.Checked ? 1 : 0;
       int result2 = form3.checkBox11.CheckState == CheckState.Checked ? 1 : 0;
       //...
       //...   
    }