A common question that routinely comes up is how to share data between forms.

I have outlined two common ways to pass data between forms, among many others. I have used a public property on the output form to accept a value and update the display. I also have a method accepting parameters from the input form, to update the display.

On the first form there are three textboxes tbFirstName, tbLastName, and tbSample with a Submit button to redirect to the output form. I have the same layout on the output form to display the passed values.

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

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //Set our variables from our input form
            string firstName = this.tbFirstName.Text;
            string lastName = this.tbLastName.Text;
            string sample = this.tbSample.Text;

            //Create an instance of the output form
            frmOutput frmOut = new frmOutput();

            //We set values through a property
            frmOut.Sample = sample;

            //We set values through a public method
            frmOut.SetDisplayValues(firstName, lastName);
           

            //We show the output form
            frmOut.ShowDialog();
        }
    }


public partial class frmOutput : Form
    {
        private string sample;
        //Getter / Setter for Sample
        public string Sample
        {
            get
            {
                return sample;
            }
            set
            {
                sample = value;

                //Set from property
                this.tbSample.Text = this.sample;
            }
        }

        public frmOutput()
        {
            InitializeComponent();
        }

        public void SetDisplayValues(string firstName, string lastName)
        {
            //Set from method
            this.tbFirstName.Text = firstName;
            this.tbLastName.Text = lastName;
        }
    }

can you please help me.... what is the code in windows form...i want to open a new form and closing or hiding my current form....

can you please help me.... what is the code in windows form...i want to open a new form and closing or hiding my current form......

friend try this show() and hide() function will help u....
eg:
to show form2 and hide form1 see below

form2.show()

and

form1.hide()

note: here form1 and form2 are the names of forms.

hii,i have tried the above code but it is not working for me.
i am having a textbox on form 1 and i am having another form which is form2.
now i want to pass this textbox value of form1 to form 2's label.so can you help me out?

Hi can anyone help me...On main form (for example Form1) I have a query which creates dynamic table in specific database...

for example

string Query = "create table"+Tablica+"(.........);";

Tablica is defined like

string Tablica = "Mujo_"+value;

where value is value from textBox1

now i want to use variable Tablica in 2nd form (for example Form2) so i can make query with insert to Tablica table

how can i do it

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.