C# 通过单击按钮将我的文本框的值从Form1传递到form2

C# 通过单击按钮将我的文本框的值从Form1传递到form2,c#,sql-server-2008,C#,Sql Server 2008,我想将文本框的值从Form1传递到Form2 此时会出现此消息 "Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog." 这是我的表格1的代码: private void btnAddReceipt_Click(object sender, EventArgs e)

我想将文本框的值从Form1传递到Form2

此时会出现此消息

"Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog."
这是我的表格1的代码

private void btnAddReceipt_Click(object sender, EventArgs e)
    {

        this.Hide();
        using (var Ticket = new frmCustomerTicket())
        {
            Ticket.CustomerID = txtCustNo.Text;
            ShowDialog();

        }

    }
这是我的Form2代码

    public string CustomerID { get; set; }


    private void frmCustomerTicket_Load(object sender, EventArgs e)
    {

        txtCustID.Text = CustomerID;        

    }
试试这个:

private void btnAddReceipt_Click(object sender, EventArgs e)
{



    this.Hide();
    var Ticket = new frmCustomerTicket();
        Ticket.CustomerID = txtCustNo.Text;
        Ticket.Show();



}
Upate
删除using块,它将导致Form2元素在超出范围时立即进行处理。

为什么不在构造函数上执行此操作? 我的意思是,你可以在你的表格2上填写:

public partial class MyForm: Form
{
   string myvar = string.Empty;
   public MyForm(string a)
   {
      InitializeComponent();
      this.myvar = a;
   }
}
在表格1中,您可以:

using (var Ticket = new frmCustomerTicket(txtCustNo.Text))
    {
        Ticket.ShowDialog();
    }

哦,第一个表单do按钮的单击事件:

        Form2 F2 = new F2(this);
        F2.Show();
        this.Hide();
然后在第二个表单中初始化第一个表单

    FormFirst F1 = new FormFirst();

    public From2(FormFirst form1)
    {
        InitializeComponent();
        F1 = form1;
    }
     textboxt2.text = F1.textbox.Text;

别忘了将第一个表单的文本框的修饰符公之于众

我想你的问题不在于表单之间传递值。我认为这是关于MDI父窗体和子窗体的。根据定义,MDI子窗体不是模态的。看看这些链接:




当我单击按钮时,它仍然显示相同的表单。我想我不明白,我以为问题是您无法显示表单。您是否从对话框表单调用ShowDialog(),或者它是一个顶级表单?它只是从一个表单发送到另一个表单的一个值。如果它只是一个文本框值,为什么不使用会话变量,如session[“txtValue”]=txtCustID.Text,并在Form2中访问此会话变量。@Gayatri,它听起来像是winforms应用程序,所以您无法使用会话