Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing Value From One Form To Another! [modified]

Passing Value From One Form To Another! [modified]

Scheduled Pinned Locked Moved C#
csharp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sr Frank
    wrote on last edited by
    #1

    Hi, I am trying to pass value from one windows form to another in desktop application using C#. I have created two text boxes on Form1 and a button, on button click I am creating Form2 and From2 contains one button, on this button click I have to pass value from Form2 to Form1 textboxes. I have created Properties in Form1. public string Emp { get { return strEmp; } set { strEmp = value; } } public string EmpID { get { return strEmpID; } set { strEmpID = value; } } And on Form1 button click: private void button1_Click(object sender, EventArgs e) { Form2 fr = new Form2(); this.textBox1.Text = strEmp ; this.textBox2.Text = strEmpID ; fr.ShowDialog(); } On Form2: private void button1_Click(object sender, EventArgs e) { Form1 fr = new Form1(); fr. Emp = "Alex"; fr. EmpID = "1121"; this.Close(); } This code is not working, Please let me know where I am wrong. Thanks,

    modified on Wednesday, March 31, 2010 12:54 AM

    L P D 3 Replies Last reply
    0
    • S Sr Frank

      Hi, I am trying to pass value from one windows form to another in desktop application using C#. I have created two text boxes on Form1 and a button, on button click I am creating Form2 and From2 contains one button, on this button click I have to pass value from Form2 to Form1 textboxes. I have created Properties in Form1. public string Emp { get { return strEmp; } set { strEmp = value; } } public string EmpID { get { return strEmpID; } set { strEmpID = value; } } And on Form1 button click: private void button1_Click(object sender, EventArgs e) { Form2 fr = new Form2(); this.textBox1.Text = strEmp ; this.textBox2.Text = strEmpID ; fr.ShowDialog(); } On Form2: private void button1_Click(object sender, EventArgs e) { Form1 fr = new Form1(); fr. Emp = "Alex"; fr. EmpID = "1121"; this.Close(); } This code is not working, Please let me know where I am wrong. Thanks,

      modified on Wednesday, March 31, 2010 12:54 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Read this article Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^]

      If you can think then I Can.

      1 Reply Last reply
      0
      • S Sr Frank

        Hi, I am trying to pass value from one windows form to another in desktop application using C#. I have created two text boxes on Form1 and a button, on button click I am creating Form2 and From2 contains one button, on this button click I have to pass value from Form2 to Form1 textboxes. I have created Properties in Form1. public string Emp { get { return strEmp; } set { strEmp = value; } } public string EmpID { get { return strEmpID; } set { strEmpID = value; } } And on Form1 button click: private void button1_Click(object sender, EventArgs e) { Form2 fr = new Form2(); this.textBox1.Text = strEmp ; this.textBox2.Text = strEmpID ; fr.ShowDialog(); } On Form2: private void button1_Click(object sender, EventArgs e) { Form1 fr = new Form1(); fr. Emp = "Alex"; fr. EmpID = "1121"; this.Close(); } This code is not working, Please let me know where I am wrong. Thanks,

        modified on Wednesday, March 31, 2010 12:54 AM

        P Offline
        P Offline
        Praveen Raghuvanshi
        wrote on last edited by
        #3

        First there is a compilation error at the 2 statements in which you are assigning variables to the textbox as name of the variables differ. Second, you are setting the values in the text box of first form after calling the showdialog. So, the values can e displayed only after you close the dialog box. Third, You are creating a new instance of form1 on the click of button on form2. It won't set the values on the initial form.

        Praveen Raghuvanshi Software Engineer, India.

        S 1 Reply Last reply
        0
        • P Praveen Raghuvanshi

          First there is a compilation error at the 2 statements in which you are assigning variables to the textbox as name of the variables differ. Second, you are setting the values in the text box of first form after calling the showdialog. So, the values can e displayed only after you close the dialog box. Third, You are creating a new instance of form1 on the click of button on form2. It won't set the values on the initial form.

          Praveen Raghuvanshi Software Engineer, India.

          S Offline
          S Offline
          Sr Frank
          wrote on last edited by
          #4

          Thanks for your time but please let me know the right solution.

          P 1 Reply Last reply
          0
          • S Sr Frank

            Thanks for your time but please let me know the right solution.

            P Offline
            P Offline
            Praveen Raghuvanshi
            wrote on last edited by
            #5

            The link provided by 'eg_Anubhava' explains the communication in a clear way.

            Praveen Raghuvanshi Software Engineer, India.

            1 Reply Last reply
            0
            • S Sr Frank

              Hi, I am trying to pass value from one windows form to another in desktop application using C#. I have created two text boxes on Form1 and a button, on button click I am creating Form2 and From2 contains one button, on this button click I have to pass value from Form2 to Form1 textboxes. I have created Properties in Form1. public string Emp { get { return strEmp; } set { strEmp = value; } } public string EmpID { get { return strEmpID; } set { strEmpID = value; } } And on Form1 button click: private void button1_Click(object sender, EventArgs e) { Form2 fr = new Form2(); this.textBox1.Text = strEmp ; this.textBox2.Text = strEmpID ; fr.ShowDialog(); } On Form2: private void button1_Click(object sender, EventArgs e) { Form1 fr = new Form1(); fr. Emp = "Alex"; fr. EmpID = "1121"; this.Close(); } This code is not working, Please let me know where I am wrong. Thanks,

              modified on Wednesday, March 31, 2010 12:54 AM

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              In addition to the article already linked - here's[^] a quick Tip that shows how to do it concisely

              Dave

              If this helped, please vote & accept answer!

              Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups