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 mainform instance

passing mainform instance

Scheduled Pinned Locked Moved C#
helpquestion
12 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.
  • M motojojo

    I'm wondering how I can pass the instance of the MainForm in my application. The situation is this in my MainForm: ... frmToolbox = new frmToolbox(**MainForm**); ... However, I don't seem to find a way to pass the form around (this doesn't work) Can anyone help? Thank you very much

    S Offline
    S Offline
    Stefan Troschuetz
    wrote on last edited by
    #2

    Declare a constructor for the frmToolBox that takes an instance of the main form

    public FormToolBox(FormMain formMain)

    and then create an instance of the tool box form as follows

    frmToolbox = new frmToolbox(this);


    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

    www.troschuetz.de

    M 1 Reply Last reply
    0
    • S Stefan Troschuetz

      Declare a constructor for the frmToolBox that takes an instance of the main form

      public FormToolBox(FormMain formMain)

      and then create an instance of the tool box form as follows

      frmToolbox = new frmToolbox(this);


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

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

      Thanks for replying however this isn't the problem, I've thought that myself. The problem is:

      Stefan Troschtz wrote:

      frmToolbox = new frmToolbox(this);

      This does NOT work as I experienced yesterday. I can't pass the MainForm around in the MainForm code using 'this' :( Problem not solved :sigh:

      S M C 3 Replies Last reply
      0
      • M motojojo

        Thanks for replying however this isn't the problem, I've thought that myself. The problem is:

        Stefan Troschtz wrote:

        frmToolbox = new frmToolbox(this);

        This does NOT work as I experienced yesterday. I can't pass the MainForm around in the MainForm code using 'this' :( Problem not solved :sigh:

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #4

        I do not get your problem. What exactly do you mean by "does not work"? Where exactly are you creating the frmToolBox instance?


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        1 Reply Last reply
        0
        • M motojojo

          Thanks for replying however this isn't the problem, I've thought that myself. The problem is:

          Stefan Troschtz wrote:

          frmToolbox = new frmToolbox(this);

          This does NOT work as I experienced yesterday. I can't pass the MainForm around in the MainForm code using 'this' :( Problem not solved :sigh:

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #5

          Hello, Am I wrong, or are you trying to use "this" in the "static void Main"? This will not work, you have to do it in the Mainforms cunstructor. All the best, Martin

          M 1 Reply Last reply
          0
          • M motojojo

            Thanks for replying however this isn't the problem, I've thought that myself. The problem is:

            Stefan Troschtz wrote:

            frmToolbox = new frmToolbox(this);

            This does NOT work as I experienced yesterday. I can't pass the MainForm around in the MainForm code using 'this' :( Problem not solved :sigh:

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #6

            Perhaps you need to do better at asking questions. If you're inside your mainform, and not inside a static function, this will work. There Application object also has a collection of open forms, which you can also iterate through and look for your mainform. But, when someone tells you how to do something,

            motojojo wrote:

            This does NOT work

            is useless. Tell us WHY it won't work, what the error is, and then we can perhaps tell you what's going on, without having to guess. Odds are high it's in a static function, and if you bothered to read the error message, you'd understand why it's not working.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            1 Reply Last reply
            0
            • M Martin 0

              Hello, Am I wrong, or are you trying to use "this" in the "static void Main"? This will not work, you have to do it in the Mainforms cunstructor. All the best, Martin

              M Offline
              M Offline
              motojojo
              wrote on last edited by
              #7

              You are right, I didn't initialize that object in the constructor Now that I did, indeed it takes 'this' as a reference, however all of my other methods are souped up since now toolbox doesn't exist enymore as a class parameter. --> The name 'toolbox' does not exist in the current context. Stuck again. I'm just stupid I guess.. Sigh :)

              M 1 Reply Last reply
              0
              • M motojojo

                You are right, I didn't initialize that object in the constructor Now that I did, indeed it takes 'this' as a reference, however all of my other methods are souped up since now toolbox doesn't exist enymore as a class parameter. --> The name 'toolbox' does not exist in the current context. Stuck again. I'm just stupid I guess.. Sigh :)

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #8

                Hello,

                motojojo wrote:

                I'm just stupid I guess..

                Give yourselfe a little more time.

                motojojo wrote:

                I didn't initialize that object in the constructor

                Ok, good so far!

                motojojo wrote:

                Now that I did, indeed it takes 'this' as a reference, however all of my other methods are souped up since now toolbox doesn't exist enymore as a class parameter.

                The "trick" is that you declare the variable outside the constructor and initialze inside.

                private SomeClass mySomeClass;

                public MainClass()
                {
                InitializeComponets();
                mySomeClass = new SomeClass(this);
                }

                Hope it helps! All the best, Martin

                M 1 Reply Last reply
                0
                • M Martin 0

                  Hello,

                  motojojo wrote:

                  I'm just stupid I guess..

                  Give yourselfe a little more time.

                  motojojo wrote:

                  I didn't initialize that object in the constructor

                  Ok, good so far!

                  motojojo wrote:

                  Now that I did, indeed it takes 'this' as a reference, however all of my other methods are souped up since now toolbox doesn't exist enymore as a class parameter.

                  The "trick" is that you declare the variable outside the constructor and initialze inside.

                  private SomeClass mySomeClass;

                  public MainClass()
                  {
                  InitializeComponets();
                  mySomeClass = new SomeClass(this);
                  }

                  Hope it helps! All the best, Martin

                  M Offline
                  M Offline
                  motojojo
                  wrote on last edited by
                  #9

                  Martin# wrote:

                  private SomeClass mySomeClass;public MainClass(){ InitializeComponets(); mySomeClass = new SomeClass(this);}

                  I've added another constructor to my Toolbox which takes no parameter so now I have Toolbox(Mainform mainform){ ... } and Toolbox(){} however, this results in an infinite loop...

                  M S 2 Replies Last reply
                  0
                  • M motojojo

                    Martin# wrote:

                    private SomeClass mySomeClass;public MainClass(){ InitializeComponets(); mySomeClass = new SomeClass(this);}

                    I've added another constructor to my Toolbox which takes no parameter so now I have Toolbox(Mainform mainform){ ... } and Toolbox(){} however, this results in an infinite loop...

                    M Offline
                    M Offline
                    motojojo
                    wrote on last edited by
                    #10

                    I've said too much. Appearantly it does work (I made an error) :) thank you guys

                    M 1 Reply Last reply
                    0
                    • M motojojo

                      Martin# wrote:

                      private SomeClass mySomeClass;public MainClass(){ InitializeComponets(); mySomeClass = new SomeClass(this);}

                      I've added another constructor to my Toolbox which takes no parameter so now I have Toolbox(Mainform mainform){ ... } and Toolbox(){} however, this results in an infinite loop...

                      S Offline
                      S Offline
                      Stefan Troschuetz
                      wrote on last edited by
                      #11

                      I think, you need to show us some more code, cause otherwise otherwise there is too much guessing in helping you. What are you doing inside the constructors? Where exactly are creating the mainform and toolbox instances? Finally, nevertheless a guess. Probably you're creating a new instance of the main form inside the tool box constructor. That is not necessary. Simply assign the instance passed in to the declared field.

                      class ToolBox
                      {
                      private MainForm mainForm;

                      public ToolBox(MainForm mainForm)
                      {
                      this.mainForm = mainForm;
                      ..
                      }
                      }


                      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                      www.troschuetz.de

                      1 Reply Last reply
                      0
                      • M motojojo

                        I've said too much. Appearantly it does work (I made an error) :) thank you guys

                        M Offline
                        M Offline
                        Martin 0
                        wrote on last edited by
                        #12

                        Hello, Glad it works now! But why do you need this second constructor? All the best, Martin

                        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