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

multiple forms

Scheduled Pinned Locked Moved C#
csharptutorial
14 Posts 5 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.
  • S Spunky Coder

    Is the form1 parent of form2 ?

    Koushik

    K Offline
    K Offline
    kabutar
    wrote on last edited by
    #5

    yes Koushik ..... form1 is the parent form and form2 is the child form.... when we click on a button in form1 the form2 will pop up..... :) thanks in advance...

    C#

    C S 2 Replies Last reply
    0
    • C Christian Graus

      kabutar wrote:

      i dont want to use delegates....

      Why not ? Delegates are the best way to do this, why are you setting out to write bad code ?

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )

      K Offline
      K Offline
      kabutar
      wrote on last edited by
      #6

      yes Christian i understand that but i want to try it this way also..... just for once so that i know how exactly it works..... just curious may be...... thanks in advance for your repeated help...:)

      C#

      1 Reply Last reply
      0
      • K kabutar

        yes Koushik ..... form1 is the parent form and form2 is the child form.... when we click on a button in form1 the form2 will pop up..... :) thanks in advance...

        C#

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

        f you want to write nasty code, you can pass a reference to form1 in to form2. Again, why won't you use a delegate ?

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )

        K N 2 Replies Last reply
        0
        • K kabutar

          yes Koushik ..... form1 is the parent form and form2 is the child form.... when we click on a button in form1 the form2 will pop up..... :) thanks in advance...

          C#

          S Offline
          S Offline
          Spunky Coder
          wrote on last edited by
          #8

          Try with the following code...but i think its not recommended to do so as you are trying to pass the reference of the form object... replace form2 constructor as follows... private Form1 m_parent; public Form2 (Form1 parentForm) { InitializeComponent(); m_parent = parentForm; } Now declare a public method in your form1 to access the textbox1.text ..now in the form2 u will be able to access the form1's textbox text by using some thing like this.. m_parent.GetForm1Text() where GetForm1Text() gives you the text of the TexBox in form1

          Koushik

          1 Reply Last reply
          0
          • C Christian Graus

            f you want to write nasty code, you can pass a reference to form1 in to form2. Again, why won't you use a delegate ?

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )

            K Offline
            K Offline
            kabutar
            wrote on last edited by
            #9

            its something like this Christian.... i wrote it with delegates but i was not very clear withy certain things in the code or how they work.....because i got the code form google... so i wanted to try it again with and without trying delegates.... since u r insisting so much that its a bad practise i think i should stick with delegates and not waste my time the other way round... thanks Chris..

            C#

            C N 2 Replies Last reply
            0
            • K kabutar

              its something like this Christian.... i wrote it with delegates but i was not very clear withy certain things in the code or how they work.....because i got the code form google... so i wanted to try it again with and without trying delegates.... since u r insisting so much that its a bad practise i think i should stick with delegates and not waste my time the other way round... thanks Chris..

              C#

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

              kabutar wrote:

              because i got the code form google...

              OK - in that case, I recommend you bookmark the MSDN site. If you find code that works for you via google, then you should read MSDN to learn how the various parts of that code work. Any source of info will do, but MSDN will give you exact definitions of the classes and methods you see being used. Delegates can be a little confusng at first, but once you get the hang of them, you'll find they are very useful.

              Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )

              1 Reply Last reply
              0
              • K kabutar

                its something like this Christian.... i wrote it with delegates but i was not very clear withy certain things in the code or how they work.....because i got the code form google... so i wanted to try it again with and without trying delegates.... since u r insisting so much that its a bad practise i think i should stick with delegates and not waste my time the other way round... thanks Chris..

                C#

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #11

                kabutar wrote:

                i wrote it with delegates but i was not very clear withy certain things in the code or how they work.....because i got the code form google...

                You should not use a code that is not fully understood. Check this example and try you can make it. I presume you created two forms say Form1 and Form2. You are going to declare delegate on Form2 as public. See the below code

                public delegate void InformParent();
                public InformParent ParentDelegate;

                ParentDelegate is an object for InformParent delegate. In this sample I have not passed any parameters. You can pass whatever values you want. Now check the below code to see how this can be invoked.

                Form2 frm = new Form2();
                frm.ParentDelegate = new WindowsApplication1.Form2.InformParent(this.InformParent);
                frm.Show();

                In this InformParent() is a private method for Form1. See below

                private void InformParent()
                {
                //Do your form1 update code
                }

                When delegate is invoked, this function will be called. When you want to inform the changes to Form1 from Form2, call ParentDelegate(), which will call method in Form1. Hope this makes it clear


                My Website | Ask smart questions

                K 1 Reply Last reply
                0
                • C Christian Graus

                  f you want to write nasty code, you can pass a reference to form1 in to form2. Again, why won't you use a delegate ?

                  Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillionOneHundredAndFortySevenMillionFourHundredAndEightyThreeThousandSixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it )

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #12

                  Christian Graus wrote:

                  f you want to write nasty code

                  But how this will be nasty ? Delegates also we are passing the function reference, in this case we are passing object reference. What is the difference ?


                  My Website | Ask smart questions

                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    kabutar wrote:

                    i wrote it with delegates but i was not very clear withy certain things in the code or how they work.....because i got the code form google...

                    You should not use a code that is not fully understood. Check this example and try you can make it. I presume you created two forms say Form1 and Form2. You are going to declare delegate on Form2 as public. See the below code

                    public delegate void InformParent();
                    public InformParent ParentDelegate;

                    ParentDelegate is an object for InformParent delegate. In this sample I have not passed any parameters. You can pass whatever values you want. Now check the below code to see how this can be invoked.

                    Form2 frm = new Form2();
                    frm.ParentDelegate = new WindowsApplication1.Form2.InformParent(this.InformParent);
                    frm.Show();

                    In this InformParent() is a private method for Form1. See below

                    private void InformParent()
                    {
                    //Do your form1 update code
                    }

                    When delegate is invoked, this function will be called. When you want to inform the changes to Form1 from Form2, call ParentDelegate(), which will call method in Form1. Hope this makes it clear


                    My Website | Ask smart questions

                    K Offline
                    K Offline
                    kabutar
                    wrote on last edited by
                    #13

                    frm.ParentDelegate = new WindowsApplication1.Form2.InformParent(this.InformParent); can you explain this code to me Navneeth.......especially this part.....new WindowsApplication1.Form2.InformParent(this.InformParent); thanks :)

                    C#

                    N 1 Reply Last reply
                    0
                    • K kabutar

                      frm.ParentDelegate = new WindowsApplication1.Form2.InformParent(this.InformParent); can you explain this code to me Navneeth.......especially this part.....new WindowsApplication1.Form2.InformParent(this.InformParent); thanks :)

                      C#

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #14

                      kabutar wrote:

                      frm.ParentDelegate = new WindowsApplication1.Form2.InformParent(this.InformParent);

                      It's nothing but initializing delegate and telling where it should call. So when you call ParentDelegate() from Form2, it will call private method InformParent() which is in Form1


                      My Website | Ask smart questions

                      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