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. Multiple Forms question

Multiple Forms question

Scheduled Pinned Locked Moved C#
questiontutorial
5 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.
  • M Offline
    M Offline
    Mutty
    wrote on last edited by
    #1

    I have an application with multiple forms, and can't figure out how to make their variables and functions visible to one another. This is what I'd like to do: namespace app1 {     public class Form1  {   public int state;   private void btn_LoadForm_Click(...)   {    Form2 f = new Form2();    f.Show();   }   public void SetState(int x)    {    state = x;    }  }  public class Form2  {   private void Form2_Load (...)   {    Form1.SetState(1);   }  } Why doesn't this work? How do I make this work? Thanks, -mutty

    G 1 Reply Last reply
    0
    • M Mutty

      I have an application with multiple forms, and can't figure out how to make their variables and functions visible to one another. This is what I'd like to do: namespace app1 {     public class Form1  {   public int state;   private void btn_LoadForm_Click(...)   {    Form2 f = new Form2();    f.Show();   }   public void SetState(int x)    {    state = x;    }  }  public class Form2  {   private void Form2_Load (...)   {    Form1.SetState(1);   }  } Why doesn't this work? How do I make this work? Thanks, -mutty

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      That doesn't work because Form1 is not an object, it's a class. You have to have a reference to the specific object to access non-static members of the class. You can send a reference to the first form in the constructor of the second form: Form2 f = new Form2(this); Add a reference in Form2 and store the reference to the first form: private Form mainForm; public Form2(Form mainForm) { this.mainForm = mainForm; } Then you can reach the first form: private void Form2_Load(...) { this.mainForm.SetState(1); } --- b { font-weight: normal; }

      M 1 Reply Last reply
      0
      • G Guffa

        That doesn't work because Form1 is not an object, it's a class. You have to have a reference to the specific object to access non-static members of the class. You can send a reference to the first form in the constructor of the second form: Form2 f = new Form2(this); Add a reference in Form2 and store the reference to the first form: private Form mainForm; public Form2(Form mainForm) { this.mainForm = mainForm; } Then you can reach the first form: private void Form2_Load(...) { this.mainForm.SetState(1); } --- b { font-weight: normal; }

        M Offline
        M Offline
        Mutty
        wrote on last edited by
        #3

        I understand all this except the line: this.mainForm = mainForm; Do I need to declare a form mainForm in form2?

        G 1 Reply Last reply
        0
        • M Mutty

          I understand all this except the line: this.mainForm = mainForm; Do I need to declare a form mainForm in form2?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          The line takes the reference to Form1 and stores it in the private variable. "this.mainForm" refers to the private variable in the Form2 class. "mainForm" refers to the parameter that you send to the constructor. --- b { font-weight: normal; }

          M 1 Reply Last reply
          0
          • G Guffa

            The line takes the reference to Form1 and stores it in the private variable. "this.mainForm" refers to the private variable in the Form2 class. "mainForm" refers to the parameter that you send to the constructor. --- b { font-weight: normal; }

            M Offline
            M Offline
            Mutty
            wrote on last edited by
            #5

            Thank you very much for the assistance. I have it working now! -mutty

            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