【问题标题】:Passing the values of my TextBox from Form1 to form2 by clicking button通过单击按钮将我的 TextBox 的值从 Form1 传递到 form2
【发布时间】:2023-04-07 09:19:01
【问题描述】:

我想将我的 TextBox 的值从 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."

这是我的Form1代码:

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;        

    }

【问题讨论】:

  • 您是否只是从一个表单发送到另一个表单的一个值。如果它只是一个文本框值,为什么不使用会话变量,如 Session["txtValue"]=txtCustID.Text 和在 Form2 中访问此会话变量。
  • @Gayatri,它听起来像是一个 winforms 应用程序,所以你将无法使用 Session

标签: c# sql-server-2008


【解决方案1】:

试试这个:

private void btnAddReceipt_Click(object sender, EventArgs e)
{



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



}

更新 删除 using 块,它将导致 Form2 元素在超出范围时立即释放。

【讨论】:

  • 当我点击按钮时,它仍然显示相同的表单。
  • 我想我不明白我认为问题是您无法显示表单。
  • 你是从一个对话框的窗体中调用 ShowDialog(),还是它是一个顶级窗体?
【解决方案2】:

为什么不在构造函数上做呢? 我的意思是你可以在你的表格 2 上有这个:

public partial class MyForm: Form
{
   string myvar = string.Empty;
   public MyForm(string a)
   {
      InitializeComponent();
      this.myvar = a;
   }
}

在你的 form1 中你可以:

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

【讨论】:

    【解决方案3】:

    哦第一种形式的按钮的点击事件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;
    

    别忘了将第一种形式的文本框的修饰符设为公开

    【讨论】:

      【解决方案4】:

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

      How can i make an MDI form inactive when child form is active
      ShowDialog with MdiParent Issue
      Call an Childform in MDIParent Form using ShowDialog()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 2014-01-02
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多