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. Change second form control text

Change second form control text

Scheduled Pinned Locked Moved C#
question
3 Posts 2 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.
  • L Offline
    L Offline
    liqnit
    wrote on last edited by
    #1

    hello i have 2 forms . how do i change form2 button control text from form1? what interface do i need to add? thanks Have a nice Day

    U 1 Reply Last reply
    0
    • L liqnit

      hello i have 2 forms . how do i change form2 button control text from form1? what interface do i need to add? thanks Have a nice Day

      U Offline
      U Offline
      User 1465389
      wrote on last edited by
      #2

      There are three steps involved in using form to form communication: 1. Create a reference to form2 from form1. 2. Set your reference equal to your instance. 3. Set your control to public. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Since we are dealing with objects here, you have to make sure they know about each other (at least form1 knows about form2). The way to do this is to create a reference to form2 from form1: public class Form1 : System.Windows.Forms.Form { private Form2 form2 = new Form2(); ...} 2. Setting your reference to the instance of form2. Something like this: private void button1_Click(object sender, System.EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); //This is where you would set you reference equal to your instance. form2 = frm2; } 3. Set the control to public (in form2): public class Form2 : System.Windows.Forms.Form { public System.Windows.Forms.Button button1; ...} NOTE: your autogenerated code should already have a definition of the button1 but it will be set to private, what you are doing here is changing it to public. Any question let me know. Peter Corcoran peter@corcoranp.com www.corcoranp.com

      L 1 Reply Last reply
      0
      • U User 1465389

        There are three steps involved in using form to form communication: 1. Create a reference to form2 from form1. 2. Set your reference equal to your instance. 3. Set your control to public. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Since we are dealing with objects here, you have to make sure they know about each other (at least form1 knows about form2). The way to do this is to create a reference to form2 from form1: public class Form1 : System.Windows.Forms.Form { private Form2 form2 = new Form2(); ...} 2. Setting your reference to the instance of form2. Something like this: private void button1_Click(object sender, System.EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); //This is where you would set you reference equal to your instance. form2 = frm2; } 3. Set the control to public (in form2): public class Form2 : System.Windows.Forms.Form { public System.Windows.Forms.Button button1; ...} NOTE: your autogenerated code should already have a definition of the button1 but it will be set to private, what you are doing here is changing it to public. Any question let me know. Peter Corcoran peter@corcoranp.com www.corcoranp.com

        L Offline
        L Offline
        liqnit
        wrote on last edited by
        #3

        Thank you very much exactly what i needed :) Have a nice Day

        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