Passing data between forms

dickersonka 1 Tallied Votes 2K Views Share

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;
        }
    }
bords 1 Light Poster

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....

bords 1 Light Poster

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......

ravikiran032 -7 Light Poster

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.

mrunmayee 0 Newbie Poster

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?

farooqaaa 48 Enthusiast
Tonchi 0 Newbie Poster

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

jamespello 0 Newbie Poster
     Form2 frm = new Form2(nTitle, nText);
     frm.Show();

     .....

    public Form2(string title,string txt)
    {
        InitializeComponent();
        this.Text = title;
        label1.Text = txt;
    }

Full source...Pass values between forms

Eldo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.