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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Is there a good example of using multiple forms.....

Is there a good example of using multiple forms.....

Scheduled Pinned Locked Moved C#
jsontutorial
18 Posts 7 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.
  • G glennPattonPub

    Great, thanks for that. But I'm looking for a way that Form1 can call up Form2, Form2 then has data that is passed back to Form1. Most of the methods I (yours included) Form1 passes the data to Form2 and not the other way I was looking at trying to alterone your methods to that but have come unstuck, is Properties approach the way to go or not and what is the overhead? All I'm think of doing is inputting three ints and away. Glenn

    realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #6

    Instead of doing it that way, you could just setup a static object that contains your data, and use that object in all of the forms. That way, when one form sets the data, the other form can read it, and change it as well.

    .45 ACP - because shooting twice is just silly
    -----
    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

    1 Reply Last reply
    0
    • G glennPattonPub

      Great, thanks for that. But I'm looking for a way that Form1 can call up Form2, Form2 then has data that is passed back to Form1. Most of the methods I (yours included) Form1 passes the data to Form2 and not the other way I was looking at trying to alterone your methods to that but have come unstuck, is Properties approach the way to go or not and what is the overhead? All I'm think of doing is inputting three ints and away. Glenn

      T Offline
      T Offline
      The Man from U N C L E
      wrote on last edited by
      #7

      If you use ShowDialog to display Form2 then after it has closed all the properties of Form2 are available to you until the instance is disposed, or goes out of scope.

      If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

      1 Reply Last reply
      0
      • D DaveyM69

        Have a look at my tip[^] then my article[^] for a more complete tutorial.

        Dave

        If this helped, please vote & accept answer!

        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        A Offline
        A Offline
        AussieLew
        wrote on last edited by
        #8

        DaveyM69 wrote:

        then my article[^] for a more complete tutorial

        Had reason to use this recently, a very helpful article from a helpful guy. Thanks again Dave!

        D 1 Reply Last reply
        0
        • A AussieLew

          DaveyM69 wrote:

          then my article[^] for a more complete tutorial

          Had reason to use this recently, a very helpful article from a helpful guy. Thanks again Dave!

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

          :thumbsup:

          Dave

          If this helped, please vote & accept answer!

          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          G 1 Reply Last reply
          0
          • D DaveyM69

            :thumbsup:

            Dave

            If this helped, please vote & accept answer!

            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            G Offline
            G Offline
            glennPattonPub
            wrote on last edited by
            #10

            Hi, Thanks for that it works one way Form1 to Form2, I am trying to get Form2 to Form1 (Don't ask Tower of Babel situation). Now I am trying the method for Form1 to Form2 reversed and running into problems. Very unpolitical correct give me Global variables back I promise not to abuse them !(like I did) Glenn

            A D 2 Replies Last reply
            0
            • G glennPattonPub

              Hi, Thanks for that it works one way Form1 to Form2, I am trying to get Form2 to Form1 (Don't ask Tower of Babel situation). Now I am trying the method for Form1 to Form2 reversed and running into problems. Very unpolitical correct give me Global variables back I promise not to abuse them !(like I did) Glenn

              A Offline
              A Offline
              AussieLew
              wrote on last edited by
              #11

              Sounds similar to what I wanted to do. Form2 needed to set a menuitem on Form1. this was done by creating the event in Form2 registering the handler in Form1. Form 2 also needed to know the status of some menuitems in Form1. This was done by creating the event in Form2 with and registering the handler in Form1. This handler sets one of the custom eventargs properties and returns them to Form2. It was a combination of Daves examples. I'm sure DaveyM69 will explain it more succintly! Thread of my question is Here may help.

              G 1 Reply Last reply
              0
              • A AussieLew

                Sounds similar to what I wanted to do. Form2 needed to set a menuitem on Form1. this was done by creating the event in Form2 registering the handler in Form1. Form 2 also needed to know the status of some menuitems in Form1. This was done by creating the event in Form2 with and registering the handler in Form1. This handler sets one of the custom eventargs properties and returns them to Form2. It was a combination of Daves examples. I'm sure DaveyM69 will explain it more succintly! Thread of my question is Here may help.

                G Offline
                G Offline
                glennPattonPub
                wrote on last edited by
                #12

                Great

                1 Reply Last reply
                0
                • G glennPattonPub

                  Hi, Thanks for that it works one way Form1 to Form2, I am trying to get Form2 to Form1 (Don't ask Tower of Babel situation). Now I am trying the method for Form1 to Form2 reversed and running into problems. Very unpolitical correct give me Global variables back I promise not to abuse them !(like I did) Glenn

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

                  Hi, sorry for the delay in getting back. I'm having a busy day at work X| It seems AussieLew has guided you through. If you need any more help feel free to post back and I will have a look tonight (UK) if you are stuck or unsure.

                  Dave

                  If this helped, please vote & accept answer!

                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  G 1 Reply Last reply
                  0
                  • D DaveyM69

                    Hi, sorry for the delay in getting back. I'm having a busy day at work X| It seems AussieLew has guided you through. If you need any more help feel free to post back and I will have a look tonight (UK) if you are stuck or unsure.

                    Dave

                    If this helped, please vote & accept answer!

                    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    G Offline
                    G Offline
                    glennPattonPub
                    wrote on last edited by
                    #14

                    He has helped but I still need a little guidance as it all seems to be forward and not backward passing. Thanks Glenn

                    D 1 Reply Last reply
                    0
                    • G glennPattonPub

                      He has helped but I still need a little guidance as it all seems to be forward and not backward passing. Thanks Glenn

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

                      glennPattonWork wrote:

                      all seems to be forward and not backward passing

                      No. Have a look at this exmple that does exactly what you want. Anything you don't understand, just ask!

                      // ThreeIntsEventArgs.cs
                      /* Arguments and delegate for FormChild.ThreeIntsEvent */

                      using System;

                      public delegate void ThreeIntsEventHandler(object sender, ThreeIntsEventArgs e);

                      public class ThreeIntsEventArgs : EventArgs
                      {
                      private int x;
                      private int y;
                      private int z;

                      public ThreeIntsEventArgs(int x, int y, int z)
                      {
                          this.x = x;
                          this.y = y;
                          this.z = z;
                      }
                      
                      public int X
                      {
                          get { return x; }
                      }
                      public int Y
                      {
                          get { return y; }
                      }
                      public int Z
                      {
                          get { return z; }
                      }
                      

                      }

                      // FormChild.cs
                      /* Click OK to raise event sending the three values */

                      using System.Drawing;
                      using System.Windows.Forms;

                      public partial class FormChild : Form
                      {
                      public event ThreeIntsEventHandler ThreeInts;

                      private NumericUpDown numericUpDownX;
                      private NumericUpDown numericUpDownY;
                      private NumericUpDown numericUpDownZ;
                      private Button buttonOK;
                      
                      public FormChild()
                      {
                          InitializeComponent();
                          // numericUpDownX
                          numericUpDownX = new NumericUpDown();
                          numericUpDownX.Location = new Point(12, 12);
                          // numericUpDownY
                          numericUpDownY = new NumericUpDown();
                          numericUpDownY.Location = new Point(12, 36);
                          // numericUpDownZ
                          numericUpDownZ = new NumericUpDown();
                          numericUpDownZ.Location = new Point(12, 60);
                          // buttonOK
                          buttonOK = new Button();
                          buttonOK.Text = "OK";
                          buttonOK.Location = new Point(12, 84);
                          buttonOK.Click += new System.EventHandler(buttonOK\_Click);
                          // Add controls
                          Controls.AddRange(new Control\[\] { numericUpDownX, numericUpDownY, numericUpDownZ, buttonOK });
                          AcceptButton = buttonOK;
                      }
                      
                      private void buttonOK\_Click(object sender, System.EventArgs e)
                      {
                          OnThreeInts(new ThreeIntsEventArgs((int)numericUpDownX.Value, (int)numericUpDownY.Value, (int)numericUpDownZ.Value));
                      }
                      protected virtual void OnThreeInts(ThreeIntsEventArgs e)
                      {
                          ThreeIntsEventHandler eh = ThreeInts;
                          if (eh != null)
                              eh(this, e);
                      }
                      

                      }

                      // FormParent.cs
                      /* Click the Form tho open a new ch

                      G 1 Reply Last reply
                      0
                      • D DaveyM69

                        glennPattonWork wrote:

                        all seems to be forward and not backward passing

                        No. Have a look at this exmple that does exactly what you want. Anything you don't understand, just ask!

                        // ThreeIntsEventArgs.cs
                        /* Arguments and delegate for FormChild.ThreeIntsEvent */

                        using System;

                        public delegate void ThreeIntsEventHandler(object sender, ThreeIntsEventArgs e);

                        public class ThreeIntsEventArgs : EventArgs
                        {
                        private int x;
                        private int y;
                        private int z;

                        public ThreeIntsEventArgs(int x, int y, int z)
                        {
                            this.x = x;
                            this.y = y;
                            this.z = z;
                        }
                        
                        public int X
                        {
                            get { return x; }
                        }
                        public int Y
                        {
                            get { return y; }
                        }
                        public int Z
                        {
                            get { return z; }
                        }
                        

                        }

                        // FormChild.cs
                        /* Click OK to raise event sending the three values */

                        using System.Drawing;
                        using System.Windows.Forms;

                        public partial class FormChild : Form
                        {
                        public event ThreeIntsEventHandler ThreeInts;

                        private NumericUpDown numericUpDownX;
                        private NumericUpDown numericUpDownY;
                        private NumericUpDown numericUpDownZ;
                        private Button buttonOK;
                        
                        public FormChild()
                        {
                            InitializeComponent();
                            // numericUpDownX
                            numericUpDownX = new NumericUpDown();
                            numericUpDownX.Location = new Point(12, 12);
                            // numericUpDownY
                            numericUpDownY = new NumericUpDown();
                            numericUpDownY.Location = new Point(12, 36);
                            // numericUpDownZ
                            numericUpDownZ = new NumericUpDown();
                            numericUpDownZ.Location = new Point(12, 60);
                            // buttonOK
                            buttonOK = new Button();
                            buttonOK.Text = "OK";
                            buttonOK.Location = new Point(12, 84);
                            buttonOK.Click += new System.EventHandler(buttonOK\_Click);
                            // Add controls
                            Controls.AddRange(new Control\[\] { numericUpDownX, numericUpDownY, numericUpDownZ, buttonOK });
                            AcceptButton = buttonOK;
                        }
                        
                        private void buttonOK\_Click(object sender, System.EventArgs e)
                        {
                            OnThreeInts(new ThreeIntsEventArgs((int)numericUpDownX.Value, (int)numericUpDownY.Value, (int)numericUpDownZ.Value));
                        }
                        protected virtual void OnThreeInts(ThreeIntsEventArgs e)
                        {
                            ThreeIntsEventHandler eh = ThreeInts;
                            if (eh != null)
                                eh(this, e);
                        }
                        

                        }

                        // FormParent.cs
                        /* Click the Form tho open a new ch

                        G Offline
                        G Offline
                        glennPattonPub
                        wrote on last edited by
                        #16

                        Hi, I have had a fiddle it seems a little over compliated at the moment. I have a simpler method using the Panel component with three text boxe sand a button click to make the panel visible, once the variables are entered the panel is made invisible the values can be recorded. What the opinion of that? I have tried in a demo App does look too bad. Glenn

                        D 2 Replies Last reply
                        0
                        • G glennPattonPub

                          Hi, I have had a fiddle it seems a little over compliated at the moment. I have a simpler method using the Panel component with three text boxe sand a button click to make the panel visible, once the variables are entered the panel is made invisible the values can be recorded. What the opinion of that? I have tried in a demo App does look too bad. Glenn

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

                          If it works for you then cool :cool: I don't see how that helps you get values from one form to another though :confused: I'm planning another article that will be a bit more light hearted so more accessible on various OOP techniques/patterns including events/delegates (Observer Pattern). I'll post here when it's published, which will probably be a couple of weeks, as you may find it useful.

                          Dave

                          If this helped, please vote & accept answer!

                          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                          1 Reply Last reply
                          0
                          • G glennPattonPub

                            Hi, I have had a fiddle it seems a little over compliated at the moment. I have a simpler method using the Panel component with three text boxe sand a button click to make the panel visible, once the variables are entered the panel is made invisible the values can be recorded. What the opinion of that? I have tried in a demo App does look too bad. Glenn

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

                            So you (and others) can see how simple events really are to create, I have posted a simple tip here[^].

                            Dave

                            If this helped, please vote & accept answer!

                            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(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