none
How to pass data from form1 to form2?

    Question

  • Got a problem in passing a value from form1's textbox to form2's textbox.
    By the way i am using mysql connection.

    Can someone help me? :) thanks
    Saturday, November 28, 2009 2:16 AM

Answers

  • Lets try following steps

    1. Create form1 and form2 object
    2. Pass the reference of each other and save it local ref
    3. Set the properties for two text box


    Here is my code

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

            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = Form2Ref.GetForm2Data;
              
            }
            public void setformRef(Form2 f)
            {
                this.Form2Ref = f;
            }
        }


    ///////////////////////////Form2/////////////////

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

            public string GetForm2Data
            {
                get { return textBox1.Text; }
            }
        }


    Main program code


      static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form1 f1 = new Form1();
                Form2 f2 = new Form2();
                f1.setformRef(f2);
                Application.Run(f1);
               
               
            }


    I have checked and this code is working . give me yr mail id i will send you complete project.  Good luck..



    • Marked as answer by nemesiscsharp Monday, November 30, 2009 1:52 AM
    Sunday, November 29, 2009 1:09 PM
  • Read this: (Old but still relevant)

    Passing Values Between Forms
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by nemesiscsharp Monday, November 30, 2009 1:52 AM
    Sunday, November 29, 2009 5:20 PM

All replies

  • Got a problem in passing a value from form1's textbox to form2's textbox.
    By the way i am using mysql connection.

    Can someone help me? :) thanks

    I think you should store instance of each form to keep the the relationship between forms. Or you can use Application.OpenForms property to find the form which you want.
    Saturday, November 28, 2009 2:24 AM
  • How sir? can you give me some example? i have a hard time just to find it out :(
    I am working with windows form application sir..
    Saturday, November 28, 2009 2:38 AM
  • How sir? can you give me some example? i have a hard time just to find it out :(
    I am working with windows form application sir..
    But  form1 and form2 in the same application or in 2 separated applications.
    Saturday, November 28, 2009 2:59 AM
  • 2 window form application sir:)
    and i have a problem in passing datas. :(
    Saturday, November 28, 2009 3:03 AM
  • Sorry for the redundancy... but is this a MDI application or two separate SDI apps
    Saturday, November 28, 2009 4:50 AM
  • I am using Windows form in the same project. Help me. :) Please? *puppy eyes*
    Sunday, November 29, 2009 2:14 AM
  • Two winform applications use the same database
    A form textbox saves data to database
    B form databind with database, and add button to update data
    Sunday, November 29, 2009 3:16 AM
  • Hi,
    You can create public property for two text box and data can be easily pass. Here is some code sample

    in form1

    public string GetText1Data
    {
    get{return Text1.text};
    }

    in form2

    public string GetText1Data
    {
    get{return Text1.text};
    }



    Now if you want to access form1 text data from form2 then call

    string x=from1.GetText1Data



    I am sure this will work..good luck



    Sunday, November 29, 2009 5:44 AM
  • I have this error sir,

    Error    2    'System.Windows.Forms.TextBox' does not contain a definition for 'text' and no extension method 'text' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)  

    hmm
    Sunday, November 29, 2009 6:55 AM
  • Just got this article

    http://www.c-sharpcorner.com/UploadFile/scottlysle/LimitedDataXfer01302007012304AM/LimitedDataXfer.aspx

    but there is an error existing here which states

    Error    1    'ViewCustomer.ViewCustTextBox1' is inaccessible due to its protection level    C:\Users\EJ\Documents\Visual Studio 2008\Projects\\UpdateCustomer.cs    42    28   

    How to solve this problem? :)
    Sunday, November 29, 2009 7:37 AM
  • Lets try following steps

    1. Create form1 and form2 object
    2. Pass the reference of each other and save it local ref
    3. Set the properties for two text box


    Here is my code

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

            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = Form2Ref.GetForm2Data;
              
            }
            public void setformRef(Form2 f)
            {
                this.Form2Ref = f;
            }
        }


    ///////////////////////////Form2/////////////////

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

            public string GetForm2Data
            {
                get { return textBox1.Text; }
            }
        }


    Main program code


      static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form1 f1 = new Form1();
                Form2 f2 = new Form2();
                f1.setformRef(f2);
                Application.Run(f1);
               
               
            }


    I have checked and this code is working . give me yr mail id i will send you complete project.  Good luck..



    • Marked as answer by nemesiscsharp Monday, November 30, 2009 1:52 AM
    Sunday, November 29, 2009 1:09 PM
  • Read this: (Old but still relevant)

    Passing Values Between Forms
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by nemesiscsharp Monday, November 30, 2009 1:52 AM
    Sunday, November 29, 2009 5:20 PM
  • Thanks sir, now i'ved got it. :)
    Cheers,
    nemesis

    This article also helps me.. :) this is for someone who have the same problem with me.. just read this :)

    http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx
    Monday, November 30, 2009 1:52 AM