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. Calling a method from another form

Calling a method from another form

Scheduled Pinned Locked Moved C#
asp-netdatabasedebugging
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.
  • D Deques

    Hello again I have two forms, one being opened by another. What I want to do is that the second form that is opened by the first one is when it is closing, I want it to run a method in first form. The code I used is something like this. private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { frmCore core = new frmCore(); core.comboRefresh("newPost"); //frmStart start = (frmStart)this.Owner; } It doesnt work. I checked with debug and it seems to run, but nothing seems to happens to the first form. Its a dropdownlist that has items in it, the second form adds new items to it in a database. the method that is called is taking data from a database and updates the dropdownlist with the new items.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    ah those vb programmers :) frmCore core = new frmCore(); creates a new instance of your mainform. so calling a method on this new instance will not affect the first form. i wouldn't do it like this anyway. my preferred solution for such things is this:

    public class frmAddCategory:Form
    {
    //your stuff for the form here
    public string NewPost
    {
    get
    {
    return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
    }
    }
    }
    public class frmCore:Form
    {
    // your code here..
    private void SomeButton_Click(object sender, EventArgs e)
    {
    frmAddCategory blah = new frmAddCategory();
    if (blah.ShowDialog() == DialogResult.OK)
    {
    myComboBox.Items.Add(blah.NewPost);
    }
    }
    }

    greets M@u

    N D 2 Replies Last reply
    0
    • D Deques

      Hello again I have two forms, one being opened by another. What I want to do is that the second form that is opened by the first one is when it is closing, I want it to run a method in first form. The code I used is something like this. private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { frmCore core = new frmCore(); core.comboRefresh("newPost"); //frmStart start = (frmStart)this.Owner; } It doesnt work. I checked with debug and it seems to run, but nothing seems to happens to the first form. Its a dropdownlist that has items in it, the second form adds new items to it in a database. the method that is called is taking data from a database and updates the dropdownlist with the new items.

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

      Deques wrote:

      frmCore core = new frmCore();

      This will create a new instance for the form and method in that instance will be called. I prefer to use delegates in this scenario. Create delegate in second form and hook a method handler from first form when second form is invoked. On second forms FormClosed event, invoke this delegate. Hope it helps

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

      D 1 Reply Last reply
      0
      • L Lost User

        ah those vb programmers :) frmCore core = new frmCore(); creates a new instance of your mainform. so calling a method on this new instance will not affect the first form. i wouldn't do it like this anyway. my preferred solution for such things is this:

        public class frmAddCategory:Form
        {
        //your stuff for the form here
        public string NewPost
        {
        get
        {
        return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
        }
        }
        }
        public class frmCore:Form
        {
        // your code here..
        private void SomeButton_Click(object sender, EventArgs e)
        {
        frmAddCategory blah = new frmAddCategory();
        if (blah.ShowDialog() == DialogResult.OK)
        {
        myComboBox.Items.Add(blah.NewPost);
        }
        }
        }

        greets M@u

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

        m@u wrote:

        public class frmAddCategory:Form
        {
        //your stuff for the form here
        public string NewPost
        {
        get
        {
        return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
        }
        }
        }
        public class frmCore:Form
        {
        // your code here..
        private void SomeButton_Click(object sender, EventArgs e)
        {
        frmAddCategory blah = new frmAddCategory();
        if (blah.ShowDialog() == DialogResult.OK)
        {
        myComboBox.Items.Add(blah.NewPost);
        }
        }
        }

        What is this ? This looks totally out of topic to me !

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

        1 Reply Last reply
        0
        • L Lost User

          ah those vb programmers :) frmCore core = new frmCore(); creates a new instance of your mainform. so calling a method on this new instance will not affect the first form. i wouldn't do it like this anyway. my preferred solution for such things is this:

          public class frmAddCategory:Form
          {
          //your stuff for the form here
          public string NewPost
          {
          get
          {
          return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
          }
          }
          }
          public class frmCore:Form
          {
          // your code here..
          private void SomeButton_Click(object sender, EventArgs e)
          {
          frmAddCategory blah = new frmAddCategory();
          if (blah.ShowDialog() == DialogResult.OK)
          {
          myComboBox.Items.Add(blah.NewPost);
          }
          }
          }

          greets M@u

          D Offline
          D Offline
          Deques
          wrote on last edited by
          #5

          Im not a VB coder, but I guess I use their method or something like that Anyway. It looks like it pass a value to a variable to the first form, which isnt what I wanted to do. I wanted to call a method. But maybe a similiar way to do like you stated above. Can you show me some sample code to call a method (function, procedure, whatever :P)

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

            Deques wrote:

            frmCore core = new frmCore();

            This will create a new instance for the form and method in that instance will be called. I prefer to use delegates in this scenario. Create delegate in second form and hook a method handler from first form when second form is invoked. On second forms FormClosed event, invoke this delegate. Hope it helps

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

            D Offline
            D Offline
            Deques
            wrote on last edited by
            #6

            How does it work? I am not familiar with delegates >.< Could you provide me some code examples, please?

            A N 2 Replies Last reply
            0
            • D Deques

              How does it work? I am not familiar with delegates >.< Could you provide me some code examples, please?

              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #7

              These may help: http://www.codeproject.com/info/search.asp?cats=3&searchkw=delegates&Submit1=Search&author=&sd=15+Nov+1999&ed=28+Nov+2007[^]

              My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -The Undefeated

              1 Reply Last reply
              0
              • D Deques

                Im not a VB coder, but I guess I use their method or something like that Anyway. It looks like it pass a value to a variable to the first form, which isnt what I wanted to do. I wanted to call a method. But maybe a similiar way to do like you stated above. Can you show me some sample code to call a method (function, procedure, whatever :P)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                sorry i thought you want to pass a value entered in frmAddCategory. for calling a method, like navaneeth says, a delegate/event would be appropriate:

                public class frmAddCategory
                {
                public event EventHandler CategoryAdded;
                protected virtual void onCategoryAdded(EventHandler e)
                {
                if (CategoryAdded != null)
                {
                CategoryAdded(this,e);
                }
                }
                private void someEventHandler(...)
                {
                onCategoryAdded(EventArgs.Empty);
                }
                //...
                }

                in the calling Form after creating frmAddCategory say myForm.CategoryAdded += new EventHandler(myForm_CategoryAdded); and in the myForm_CategoryAdded you can then add the Entry to the Combobox.

                1 Reply Last reply
                0
                • D Deques

                  How does it work? I am not familiar with delegates >.< Could you provide me some code examples, please?

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

                  Deques wrote:

                  I am not familiar with delegates

                  Delegates are function pointers in which you can assign a function's reference, and when delegate is invoked, supplied function will be called. For explaining this we have two forms say Form1 and Form2. I am invoking Form2 from Form1's load event. Inside Form2 I have declared a delegate. See the Form2 code below

                  public class Form2 : System.Windows.Forms.Form
                  {
                  public delegate void FormClosed();
                  public FormClosed FormClosedHandler;

                  private void Form2\_Closed(object sender, System.EventArgs e)
                  {
                  	FormClosedHandler();	//Invoking delegate
                  }
                  

                  }

                  In the above code, you can see I have created a FormClosed() delegate and an object for that delegate. This object I will be assigning from the Form1 just before Form2 is shown. See the Form1 code below

                  public class Form1 : System.Windows.Forms.Form
                  {
                  private void Form1_Load(object sender, System.EventArgs e)
                  {
                  Form2 obj = new Form2();
                  obj.FormClosedHandler += new Form2.FormClosed(this.Form2ClosedHandler);
                  obj.Show();
                  }
                  }

                  In this I have assigned a private method Form2ClosedHandler to the delegate. So when delegate is invoked, this private method in Form1 will get called. You can refresh your combo inside this method. Delegates are good for communicating between classes. Hope this helps

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                  D 1 Reply Last reply
                  0
                  • D Deques

                    Hello again I have two forms, one being opened by another. What I want to do is that the second form that is opened by the first one is when it is closing, I want it to run a method in first form. The code I used is something like this. private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { frmCore core = new frmCore(); core.comboRefresh("newPost"); //frmStart start = (frmStart)this.Owner; } It doesnt work. I checked with debug and it seems to run, but nothing seems to happens to the first form. Its a dropdownlist that has items in it, the second form adds new items to it in a database. the method that is called is taking data from a database and updates the dropdownlist with the new items.

                    N Offline
                    N Offline
                    Nouman Bhatti
                    wrote on last edited by
                    #10

                    implement delegate and raise event. the problem in your code it's making the new copy of your form and you need to perform some action on your previously opened form

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

                      Deques wrote:

                      I am not familiar with delegates

                      Delegates are function pointers in which you can assign a function's reference, and when delegate is invoked, supplied function will be called. For explaining this we have two forms say Form1 and Form2. I am invoking Form2 from Form1's load event. Inside Form2 I have declared a delegate. See the Form2 code below

                      public class Form2 : System.Windows.Forms.Form
                      {
                      public delegate void FormClosed();
                      public FormClosed FormClosedHandler;

                      private void Form2\_Closed(object sender, System.EventArgs e)
                      {
                      	FormClosedHandler();	//Invoking delegate
                      }
                      

                      }

                      In the above code, you can see I have created a FormClosed() delegate and an object for that delegate. This object I will be assigning from the Form1 just before Form2 is shown. See the Form1 code below

                      public class Form1 : System.Windows.Forms.Form
                      {
                      private void Form1_Load(object sender, System.EventArgs e)
                      {
                      Form2 obj = new Form2();
                      obj.FormClosedHandler += new Form2.FormClosed(this.Form2ClosedHandler);
                      obj.Show();
                      }
                      }

                      In this I have assigned a private method Form2ClosedHandler to the delegate. So when delegate is invoked, this private method in Form1 will get called. You can refresh your combo inside this method. Delegates are good for communicating between classes. Hope this helps

                      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                      D Offline
                      D Offline
                      Deques
                      wrote on last edited by
                      #11

                      Thanks for the information I have tried this code, but when I build it I get this error the private method I have assigned with. I thnk I have done something wrong It says "Method name expected" This is the code I used for form 1

                          private void btnNewPost\_Click(object sender, EventArgs e)
                          {
                              Forms.frmAddCategory addcats = new frmAddCategory();
                              addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost"));
                              addcats.ShowDialog();
                          }
                      

                      comboRefresh(string blah)
                      {
                      //code to refresh the combobox
                      }

                      and this is for the form 2

                      public partial class frmAddCategory : Form
                      {
                          //Delegate
                          public delegate void refreshcat();
                          public refreshcat Refresher;
                      
                          private void frmAddCategory\_FormClosed(object sender, FormClosedEventArgs e)
                          {
                              Refresher();
                          }
                      
                      N 1 Reply Last reply
                      0
                      • D Deques

                        Thanks for the information I have tried this code, but when I build it I get this error the private method I have assigned with. I thnk I have done something wrong It says "Method name expected" This is the code I used for form 1

                            private void btnNewPost\_Click(object sender, EventArgs e)
                            {
                                Forms.frmAddCategory addcats = new frmAddCategory();
                                addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost"));
                                addcats.ShowDialog();
                            }
                        

                        comboRefresh(string blah)
                        {
                        //code to refresh the combobox
                        }

                        and this is for the form 2

                        public partial class frmAddCategory : Form
                        {
                            //Delegate
                            public delegate void refreshcat();
                            public refreshcat Refresher;
                        
                            private void frmAddCategory\_FormClosed(object sender, FormClosedEventArgs e)
                            {
                                Refresher();
                            }
                        
                        N Offline
                        N Offline
                        N a v a n e e t h
                        wrote on last edited by
                        #12

                        Deques wrote:

                        addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost"));

                        addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh);

                        If your comboRefresh() method takes string parameter, you should change the delegate like

                        Deques wrote:

                        public delegate void refreshcat();

                        public delegate void refreshcat(string str);

                        Deques wrote:

                        private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { Refresher(); }

                        private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e)
                        {
                        Refresher("string to be passed");
                        }

                        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

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

                          Deques wrote:

                          addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost"));

                          addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh);

                          If your comboRefresh() method takes string parameter, you should change the delegate like

                          Deques wrote:

                          public delegate void refreshcat();

                          public delegate void refreshcat(string str);

                          Deques wrote:

                          private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { Refresher(); }

                          private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e)
                          {
                          Refresher("string to be passed");
                          }

                          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

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

                          Thanks. It works now :D Only thing that the combobox doesnt update directly sometimes. But thats another problem

                          N 1 Reply Last reply
                          0
                          • D Deques

                            Thanks. It works now :D Only thing that the combobox doesnt update directly sometimes. But thats another problem

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

                            Welcome. Glad to hear that it helped

                            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia 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