-2

I am trying to transfer information from form B into form A. Basically formA has a button that opens a new form (form B), and then form B has a textbox where you enter text. Then when you close that form B (via a close button) a textbox in form A should be updated with text inputted in form B.

However its not working, textbox in form A is not recieving the text entered in form B, its giving me a null value.

//main class of Form A (the one that has to recieve into from Form B)
public partial class FormManager : Form
    {
        //creating an instance of Form B
        FormContact contactForm;
        public string a;

        public FormManager()
        {
            InitializeComponent();
            ControlsDisabled();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //trying to fill in a textbox from form B into form A
            contactForm = new FormContact();
            contactForm.Show();
            this.Refresh();
            txtFname.Text = contactForm.fname;
            //^^^the main problem, this value is null
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            contactForm = new FormContact(txtFname.Text, txtLname.Text);
            contactForm.Show();
        }

//main class of form B(the form that has to give info to form A)
 public partial class FormContact : Form
    {
       public string fname;
       public string lname;

       public FormContact()
       {
           InitializeComponent();
       }

       private void btnSave_Click(object sender, EventArgs e)
       {
          fname = txtFnameA.Text;
          lname = txtLname.Text;
          this.Refresh();
          this.Close();
       }
       public FormContact(string Fname, string Lname)
       {
           InitializeComponent();
           txtFnameA.Text = Fname;
           txtLname.Text = Lname;
       }

    }
bubblepop
  • 43
  • 5
  • 1
    https://stackoverflow.com/questions/4587952/passing-data-between-forms - When you're using `.Close()` , you are killing the source of the data. Try using `this.Hide();` instead. – Jaskier Jul 26 '19 at 13:48

3 Answers3

0

You're reading the fname value immediately after showing the FormContact before the user has had a chance to enter a value. Try the following:

contactForm.ShowDialog();

txtFname.Text = contactForm.fname;
Michael Csikos
  • 688
  • 9
  • 11
0

The easiest way for you would be to use contactForm.ShowDialog(), with this you can assign one Button the Dialog.OK Property to make it close the contactForm and go back to your Form A. To make it even simpler make the elements in your contactForm Public so you can type something like txtFname.text = contactForm.txtUsername (of course this comes after contactForm.ShowDialog()).

0

Open your contactForm like this:

using (var contactForm = new FormContact())
{
    if (contactForm.ShowDialog() == DialogResult.OK)
    {                    
        txtFname.Text = contactForm.fname;              
    }    
}

Otherwise your formContact dialog object is released before you can touch it's properties.

Dogmeat
  • 7
  • 4