michelle
michelle

Reputation: 2807

passing parameter values from parent and child forms c#

I have two forms, one with a button 'Add' which loads the second form with two textboxes and a submit button.

First of all I need a way of passing the text box values to form 1 (parent) and making form 2 close on the submit..

How can I do this? Until now I have written this code but it is not working

 private void button1_Click(object sender, EventArgs e)
        {

            emailForm EmailF = new emailForm();
            if ((EmailF.Username != null &&  EmailF.Password != null)) 
            {
                string user = EmailF.Username;
                string pass = EmailF.Password;

            }

and in the emailForm.cs

 private void button1_Click(object sender, EventArgs e)
        {
            username = username_textbox.Text;
            password = username_textbox.Text;
            Close();

        }

        public string Username
        {
            get { return username; }
            set { this.username = value; }
        }

        public string Password
        {
            get { return password;}
            set { this.password = value; }
        }

Upvotes: 0

Views: 2809

Answers (2)

Mohamed Abed
Mohamed Abed

Reputation: 5123

public void ShowMyDialogBox()
{
   Form2 testDialog = new Form2();

   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if (testDialog.ShowDialog(this) == DialogResult.OK)
   {
      // Read the contents of testDialog's TextBox.
      this.txtResult.Text = testDialog.TextBox1.Text;
   }
   else
   {
      this.txtResult.Text = "Cancelled";
   }
   testDialog.Dispose();
}

Upvotes: 0

qJake
qJake

Reputation: 17139

You need to look at Form.ShowDialog(). This will do what you want, and when the user closes the dialog window you can make sure they clicked "OK" (or whatever else) and then grab the values from the form.

Upvotes: 2

Related Questions