C#:  How to pass data between forms

trevor1940
trevor1940 used Ask the Experts™
on
Hi I'm trying to work out how to pass variables between forms

Expanding on this tuttorial



How is form 2 made active and form 1 not clickable?

If form2 also has a textbox1 and an OK button

on clicking OK How do you pass form2.textbox1.text  back to form1.textbox1.text

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PassBetweenForms
{
    public partial class Form1 : Form
    {
        public static string SetValueForText1 = "";

        public static string SetValueForText2 = "";
        public static string SetValueForText3 = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetValueForText1 = textBox1.Text;
            SetValueForText2 = textBox2.Text;
            SetValueForText3 = textBox3.Text;

            Form2 frm2 = new Form2();
            frm2.Show();
            textBox1.Text = frm2.textBox1Text;
        }
    }
}

Open in new window


Form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PassBetweenForms
{
    public partial class Form2 : Form
    {
        public static string textBox1Text = ""; 
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = Form1.SetValueForText1;
            label2.Text = Form1.SetValueForText2;
            label3.Text = Form1.SetValueForText3;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1Text = textBox1.Text; // error
             //  Member 'Form2.textBox1Text' cannot be accessed with an instance reference; qualify it with a type name instead	PassBetweenForms	d:\VB\Test\PassBetweenForms\PassBetweenForms\Form1.cs	32	Active

        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Consultant
Top Expert 2016
Commented:
>>How is form 2 made active and form 1 not clickable?

Change frm2.Show(); for frm2.ShowDialog();

>>on clicking OK How do you pass form2.textbox1.text  back to form1.textbox1.text

a better way of doing it would be:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.SetValues(textBox1.Text, textBox2.Text, textBox3.Text);
        frm2.ShowDialog();
        textBox1.Text = frm2.GetText1Value();
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }


    public void SetValues(string textBox1Text, string textBox2Text, string textBox3Text)
    {
        label1.Text = textBox1Text;
        label2.Text = textBox2Text;
        label3.Text = textBox3Text;
    }

    public string GetText1Value()
    {
        return textBox1.Text;
    }
}

Open in new window

Author

Commented:
Hi
Thanx this works  when you "X" off form2 or form2 closes the value is returned back to form1

Is there a way of trapping it with an OK button?
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
you can set add DialogResult to OK like this:

on form2, add this:
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

Open in new window


and change form1 for:
private void button1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.SetValues(textBox1.Text, textBox2.Text, textBox3.Text);
    if (frm2.ShowDialog() == DialogResult.OK)
        textBox1.Text = frm2.GetText1Value();
    else
        textBox1.Text = "Canceled";
}

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Now I'm confused

I have hit the form2 button twice for it to close?
first time the form jumps
second time bellow is run

        public string GetText1Value()
        {
            return textBox1.Text;
        }

Open in new window

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
you surely have written other code or set other properties. I just retried my sample and it works first time!

Author

Commented:
Not sure how or why
Created a clean win forms App and it worked
So I've clearly set a property somewhere but can't locate it probably because I don't know what I'm looking for

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial