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. How do I load a new form and access varables from it?

How do I load a new form and access varables from it?

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 3 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 want to bring up a second form to adjust variables on the first form. Any help appreciated! -mutty

    M 1 Reply Last reply
    0
    • M Mutty

      I want to bring up a second form to adjust variables on the first form. Any help appreciated! -mutty

      M Offline
      M Offline
      Mohamad Al Husseiny
      wrote on last edited by
      #2

      First Is This Variable Public if Yes You Can Do Somthing Like That in Form1

      Form2 form2=new Form2();
      form2.variableName=value;
      

      If this variable is not public which will be better You can Write public function in the form and make it set the value of the variable you can code somthing like thar in Form2 which have the variable you wwant to access

      public voide SetValue(int i)
      {
      myVariable=i;
      }
      

      and in Form1 You can Write

      Form2 form=new Form2();
      form2.SetValue(1);
      

      MCAD

      M 1 Reply Last reply
      0
      • M Mohamad Al Husseiny

        First Is This Variable Public if Yes You Can Do Somthing Like That in Form1

        Form2 form2=new Form2();
        form2.variableName=value;
        

        If this variable is not public which will be better You can Write public function in the form and make it set the value of the variable you can code somthing like thar in Form2 which have the variable you wwant to access

        public voide SetValue(int i)
        {
        myVariable=i;
        }
        

        and in Form1 You can Write

        Form2 form=new Form2();
        form2.SetValue(1);
        

        MCAD

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

        Sorry, I should have been clearer what I mean. I am trying to open a new form, (Form2), from my main form (form1) On form2 are numerous controls which should change variables of form1.

        A M 2 Replies Last reply
        0
        • M Mutty

          Sorry, I should have been clearer what I mean. I am trying to open a new form, (Form2), from my main form (form1) On form2 are numerous controls which should change variables of form1.

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Pass a reference to form1 in form2's constructor and store it inside form2: in form 1: Form2 form2 = new Form2(this); form2.Show(); in form 2: public Form2(Form1 refToForm) { this.form1 = refToForm; } private someMethod() { refToForm.SomeProperty = someValue; }

          1 Reply Last reply
          0
          • M Mutty

            Sorry, I should have been clearer what I mean. I am trying to open a new form, (Form2), from my main form (form1) On form2 are numerous controls which should change variables of form1.

            M Offline
            M Offline
            Mohamad Al Husseiny
            wrote on last edited by
            #5

            Mutty wrote: am trying to open a new form, (Form2), from my main form (form1) In Form1

            Form2 form2=new Form2();
            form2.Show();
            Mutty wrote:
            On form2 are numerous controls which should change variables of form1
            in form2 like in my other message
            
            Form1 form1=new Form1();
            form1.var=value;
            

            MCAD

            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