简体   繁体   中英

Passing the values of my TextBox from Form1 to form2 by clicking button

I want to pass values of my TextBox from Form1 to Form2.

And this message appear.

"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."

Here is my Code of Form1 :

private void btnAddReceipt_Click(object sender, EventArgs e)
    {

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

        }

    }

Here is My Code in Form2

    public string CustomerID { get; set; }


    private void frmCustomerTicket_Load(object sender, EventArgs e)
    {

        txtCustID.Text = CustomerID;        

    }

Try this:

private void btnAddReceipt_Click(object sender, EventArgs e)
{



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



}

Upate Remove the using block it will cause the Form2 element to dispose as soon as it falls out of scope.

Why don't you do it on the constructor? I mean you can have this on ur form 2:

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

and in your form1 you could have:

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

oh the click event of the button of first form do:

        Form2 F2 = new F2(this);
        F2.Show();
        this.Hide();

then in second form initialize the first form

    FormFirst F1 = new FormFirst();

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

Don't forget to make the modifier of the textbox of first form to public

I think your problem is not about forms. 表单I think it's about MDI Parent & Child forms. By definition, an MDI child form is not modal. Take a look at these links:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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