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. call an opener form method

call an opener form method

Scheduled Pinned Locked Moved C#
question
17 Posts 6 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 mabrahao

    hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!

    B Offline
    B Offline
    Blue_Boy
    wrote on last edited by
    #5

    you can do it something like: using(Form2 frm2 = new Form2()) { frm2.myVoid(); }


    I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

    L 1 Reply Last reply
    0
    • P Paladin2000

      This works for me. Two forms, one with a button and textbox (Form1), the other just a button (Form2).

      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }

      private void button1\_Click(object sender, EventArgs e)
      {
          Form2 form2 = new Form2(this);
          form2.Show();
      }
      
      public void Change(string text)
      {
          textBox1.Text = text;
      }
      

      }

      public partial class Form2 : Form
      {
      public Form2()
      {
      InitializeComponent();
      }

      public Form2(Form1 form1) : this()
      {
          \_form1 = form1;
      }
      
      private Form1 \_form1;
      
      private void button1\_Click(object sender, EventArgs e)
      {
          \_form1.Change("Put some text in the TextBox");
      }
      

      }

      M Offline
      M Offline
      mabrahao
      wrote on last edited by
      #6

      ok, almost there... i got only one more problem... i need to pass 2 param to my form2. ex: From2 form = new Form2(id, this);

      P 1 Reply Last reply
      0
      • M mabrahao

        ok, almost there... i got only one more problem... i need to pass 2 param to my form2. ex: From2 form = new Form2(id, this);

        P Offline
        P Offline
        Paladin2000
        wrote on last edited by
        #7

        Just change the constructor... Simple.

        public Form2(int id, Form1 form1) : this()
        {
        _id = id;
        _form1 = form1;
        }

        private int _id;
        private Form1 _form1;

        1 Reply Last reply
        0
        • P Paladin2000

          This works for me. Two forms, one with a button and textbox (Form1), the other just a button (Form2).

          public partial class Form1 : Form
          {
          public Form1()
          {
          InitializeComponent();
          }

          private void button1\_Click(object sender, EventArgs e)
          {
              Form2 form2 = new Form2(this);
              form2.Show();
          }
          
          public void Change(string text)
          {
              textBox1.Text = text;
          }
          

          }

          public partial class Form2 : Form
          {
          public Form2()
          {
          InitializeComponent();
          }

          public Form2(Form1 form1) : this()
          {
              \_form1 = form1;
          }
          
          private Form1 \_form1;
          
          private void button1\_Click(object sender, EventArgs e)
          {
              \_form1.Change("Put some text in the TextBox");
          }
          

          }

          M Offline
          M Offline
          mabrahao
          wrote on last edited by
          #8

          solved... just removed the : this(). anyway... thank you very much

          P 1 Reply Last reply
          0
          • M mabrahao

            solved... just removed the : this(). anyway... thank you very much

            P Offline
            P Offline
            Paladin2000
            wrote on last edited by
            #9

            The this() call makes the constuctor call the lower-level constructor overload. In this example, Form2(Form1 form1) : this() causes it to call public Form2() during its execution, which thereby causes it to call InitializeComponent(); (and anything else inside Form2()). If you don't have a parameter-less constructor overload of course, it will fail. ;)

            public Form2()
            {
            InitializeComponent();
            }

            public Form2(Form1 form1) : this()
            {
            _form1 = form1;
            }

            1 Reply Last reply
            0
            • M mabrahao

              hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #10

              Either do the way others have suggested or follow on of the following approach: 1. Create a class called Common or something similar and place the methods used by more than one forms there. Then all the forms can call methods in this class. Or 2. Have a base class that contains common methods (possibly marked virtual) and all forms in your application inherit from that class.

              "Your code will never work, Luc's always will.", Richard MacCutchan[^]

              1 Reply Last reply
              0
              • B Blue_Boy

                you can do it something like: using(Form2 frm2 = new Form2()) { frm2.myVoid(); }


                I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #11

                your answer is not clear at all. If this is a third form you are creating, it is wrong as it would not share anything with the original Form it is trying to interact with. :|

                Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

                Season's Greetings to all CPians.

                1 Reply Last reply
                0
                • M mabrahao

                  hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!

                  F Offline
                  F Offline
                  fjdiewornncalwe
                  wrote on last edited by
                  #12

                  I never like solutions that include a child knowing about its parent. I question whether the UI forms should be communicating directly in the first place and if some re-architecturing needs to take place on that design. But suffice to say, if the forms do need to communicate, I would much rather see it done where Form2 raises an event for which Form1 creates a handler when creating Form2. That way, at least the child still knows nothing about the parent. if( eventToCall != null ) eventToCall();

                  I wasn't, now I am, then I won't be anymore.

                  P 1 Reply Last reply
                  0
                  • F fjdiewornncalwe

                    I never like solutions that include a child knowing about its parent. I question whether the UI forms should be communicating directly in the first place and if some re-architecturing needs to take place on that design. But suffice to say, if the forms do need to communicate, I would much rather see it done where Form2 raises an event for which Form1 creates a handler when creating Form2. That way, at least the child still knows nothing about the parent. if( eventToCall != null ) eventToCall();

                    I wasn't, now I am, then I won't be anymore.

                    P Offline
                    P Offline
                    Paladin2000
                    wrote on last edited by
                    #13

                    With events instead, it could look something like this. (Probably better to pass the message in an event argument, though.)

                    public partial class Form1 : Form
                    {
                    public Form1()
                    {
                    InitializeComponent();
                    form2.Clicked += new EventHandler(form2_Clicked);
                    }

                    private void form2\_Clicked(object sender, EventArgs e)
                    {
                        textBox1.Text = form2.Message;
                    }
                    
                    private void button1\_Click(object sender, EventArgs e)
                    {
                        form2.Show();
                    }
                    
                    private Form2 form2 = new Form2();
                    

                    }

                    public partial class Form2 : Form
                    {
                    public Form2()
                    {
                    InitializeComponent();
                    }

                    private void button1\_Click(object sender, EventArgs e)
                    {
                        Message = "Here is some text";
                        if (Clicked != null) Clicked(sender, e);
                    }
                    
                    public string Message = string.Empty;
                    
                    public event EventHandler Clicked;
                    

                    }

                    [Edit] Public to private on Form2.

                    modified on Wednesday, January 5, 2011 1:31 PM

                    F 1 Reply Last reply
                    0
                    • P Paladin2000

                      With events instead, it could look something like this. (Probably better to pass the message in an event argument, though.)

                      public partial class Form1 : Form
                      {
                      public Form1()
                      {
                      InitializeComponent();
                      form2.Clicked += new EventHandler(form2_Clicked);
                      }

                      private void form2\_Clicked(object sender, EventArgs e)
                      {
                          textBox1.Text = form2.Message;
                      }
                      
                      private void button1\_Click(object sender, EventArgs e)
                      {
                          form2.Show();
                      }
                      
                      private Form2 form2 = new Form2();
                      

                      }

                      public partial class Form2 : Form
                      {
                      public Form2()
                      {
                      InitializeComponent();
                      }

                      private void button1\_Click(object sender, EventArgs e)
                      {
                          Message = "Here is some text";
                          if (Clicked != null) Clicked(sender, e);
                      }
                      
                      public string Message = string.Empty;
                      
                      public event EventHandler Clicked;
                      

                      }

                      [Edit] Public to private on Form2.

                      modified on Wednesday, January 5, 2011 1:31 PM

                      F Offline
                      F Offline
                      fjdiewornncalwe
                      wrote on last edited by
                      #14

                      In theory correct, but there may be a few issues. public Form2 form2 = new Form2(); should probably be made a private member unless it it required by something outside this form.(That would likely not be a good idea) Other than that, I think it is pretty close.

                      I wasn't, now I am, then I won't be anymore.

                      P 1 Reply Last reply
                      0
                      • F fjdiewornncalwe

                        In theory correct, but there may be a few issues. public Form2 form2 = new Form2(); should probably be made a private member unless it it required by something outside this form.(That would likely not be a good idea) Other than that, I think it is pretty close.

                        I wasn't, now I am, then I won't be anymore.

                        P Offline
                        P Offline
                        Paladin2000
                        wrote on last edited by
                        #15

                        Did I say public? I meant private... :-O :-D

                        F 1 Reply Last reply
                        0
                        • M mabrahao

                          hi guys, i have a form that needs to call a function on his opener form. How can i do this? ex: form1 opens form2, and form2 needs to call a method in form1. thanks a lot!

                          P Offline
                          P Offline
                          Paladin2000
                          wrote on last edited by
                          #16

                          Might as well give the "event" version that passes the string as an event arg...

                          public partial class Form1 : Form
                          {
                          public Form1()
                          {
                          InitializeComponent();
                          form2.Clicked += new Form2.StringEventHandler(form2_Clicked);
                          }

                          private void form2\_Clicked(string message)
                          {
                              textBox1.Text = message;
                          }
                          
                          private void button1\_Click(object sender, EventArgs e)
                          {
                              form2.Show();
                          }
                          
                          private Form2 form2 = new Form2();
                          

                          }

                          public partial class Form2 : Form
                          {
                          public Form2()
                          {
                          InitializeComponent();
                          }

                          private void button1\_Click(object sender, EventArgs e)
                          {
                              if (Clicked != null) Clicked("Here is some text");
                          }
                          
                          public event StringEventHandler Clicked;
                          public delegate void StringEventHandler(string message);
                          

                          }

                          1 Reply Last reply
                          0
                          • P Paladin2000

                            Did I say public? I meant private... :-O :-D

                            F Offline
                            F Offline
                            fjdiewornncalwe
                            wrote on last edited by
                            #17

                            Hide your members whenever possible... Showing them almost always leads to embarrassment and ridicule... :)

                            I wasn't, now I am, then I won't be anymore.

                            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