none
Call method from other form to string variables RRS feed

  • Question

  • Hi, i would like to ask is it possible to call a method from other form and store inside a string?

    e.g.   Controller c = new Controller();

    string host = c.RetrieveComputers();

    thanks!

    Wednesday, September 18, 2013 2:59 PM

Answers

  • Yes, it is possible. Here is a simple example.

    Create a new project. Add this code to the Form1:

            private void Form1_Load(object sender, EventArgs e)
            {
                this.Text = "Custom message";
                new Form2().Show();
            }
    
            public string GetFormCaption()
            {
                return this.Text;
            }

    Then create a new form called Form2. Add a button and this code to the button:

            private void button1_Click(object sender, EventArgs e)
            {
                //Get a list of all open forms in application
                FormCollection MyForms = Application.OpenForms; 
    
                //Loop through to find the form you're looking for
                foreach (Form MyForm in MyForms)
                {
                    //If the current form is a Form1 object
                    if (MyForm.GetType() == typeof(Form1))
                    {
                        //Cast the Form to Form1 and call your function
                        MessageBox.Show(((Form1)MyForm).GetFormCaption());
                        break;
                    }
                }
            }

    Then run the application. Find the Form 2 window. Click the button and you should see the message retrieved from the function on your Form1.

    The important thing to remember is that your forms are classes. You have to call the function on the instantiated form, not the form type itself. Looping through the open forms allows you to do this by finding the type and instance of the form you seek. If you have multiple open forms of the same type, you can also check the name or other properties of the form instance to see if you are calling the function on the correct instance of the form.


    Wednesday, September 18, 2013 6:18 PM

All replies

  • Yes, it is possible. Here is a simple example.

    Create a new project. Add this code to the Form1:

            private void Form1_Load(object sender, EventArgs e)
            {
                this.Text = "Custom message";
                new Form2().Show();
            }
    
            public string GetFormCaption()
            {
                return this.Text;
            }

    Then create a new form called Form2. Add a button and this code to the button:

            private void button1_Click(object sender, EventArgs e)
            {
                //Get a list of all open forms in application
                FormCollection MyForms = Application.OpenForms; 
    
                //Loop through to find the form you're looking for
                foreach (Form MyForm in MyForms)
                {
                    //If the current form is a Form1 object
                    if (MyForm.GetType() == typeof(Form1))
                    {
                        //Cast the Form to Form1 and call your function
                        MessageBox.Show(((Form1)MyForm).GetFormCaption());
                        break;
                    }
                }
            }

    Then run the application. Find the Form 2 window. Click the button and you should see the message retrieved from the function on your Form1.

    The important thing to remember is that your forms are classes. You have to call the function on the instantiated form, not the form type itself. Looping through the open forms allows you to do this by finding the type and instance of the form you seek. If you have multiple open forms of the same type, you can also check the name or other properties of the form instance to see if you are calling the function on the correct instance of the form.


    Wednesday, September 18, 2013 6:18 PM
  • Hi

    Its very simple,you need to assign Public modifier to that method RetriveComputers

    public string RetrieveComputers()

    {

    //Your stuff

    }


    Happy Coding, RDRaja

    Thursday, September 19, 2013 5:49 PM