0

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;        

    }
2
  • Is it only one value that you are sending from one form to another.If it is only one textbox value why don't you use session variable like Session["txtValue"]=txtCustID.Text and access this session variable in Form2.
    – Gayatri
    Jul 12, 2013 at 5:03
  • @Gayatri, it sounds like its a winforms application so you wont be able to use Session Jul 12, 2013 at 6:48

4 Answers 4

0

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.

3
  • When I click the button it still display the same Form. Jul 12, 2013 at 0:55
  • I guess I dont understand I thought the problem was you couldnt display the form.
    – Wjdavis5
    Jul 12, 2013 at 1:00
  • Are you calling ShowDialog() from a form that is a Dialog, or is it a top level form?
    – Wjdavis5
    Jul 12, 2013 at 1:04
0

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();
    }
0

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

0

I think your problem is not about passing values between 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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.