locked
How do you access the methods within that form from a newly created form? RRS feed

  • Question

  • Hi, I just have a quick question. When you add a new form, how do you access the methods within that form? Do you create and object of the form? thanks in advance for all the help.
    Wednesday, June 24, 2009 6:03 PM

Answers

  • Normally, it is not considered "best practices" to do this.

    But if you really need to, you can make the methods public, then call them from one form to another:

                Form2 frm2 = new Form2();
                frm2.Show();
                frm2.MyMethodCall();
    Best practice would dictate putting the method into an appropriate class. Then call the method on an instance of the class from either form.

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Aland Li Thursday, July 2, 2009 11:28 AM
    Wednesday, June 24, 2009 6:10 PM
  • Hi Otis,

    From my experience, you can define a public function in xml form and call that function in main form to populate the combo box. This is the code snippet: 

    Main form:
    private void MainForm_Load(object sender, EventArgs e)

    {

        //initial the list view.

        this.listView1.Items.Add("01");

        this.listView1.Items.Add("02");

     

        //Initial the xml form and show it.

        XmlForm form = new XmlForm();

        form.PopulateComboBox(this.listView1.Items);

        form.Show();
    }


    Xml Form:
           //Use this method to populate the combo box.

           public void PopulateComboBox(ListView.ListViewItemCollection items)

           {

               foreach (ListViewItem item in items)

               {

                   this.comboBox1.Items.Add(item.Text);

               }
           }


    Let me know if this helps.
    Aland Li

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Edited by Aland Li Wednesday, July 1, 2009 12:05 PM
    • Marked as answer by Aland Li Thursday, July 2, 2009 11:28 AM
    Friday, June 26, 2009 8:43 AM

All replies

  • Normally, it is not considered "best practices" to do this.

    But if you really need to, you can make the methods public, then call them from one form to another:

                Form2 frm2 = new Form2();
                frm2.Show();
                frm2.MyMethodCall();
    Best practice would dictate putting the method into an appropriate class. Then call the method on an instance of the class from either form.

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by Aland Li Thursday, July 2, 2009 11:28 AM
    Wednesday, June 24, 2009 6:10 PM
  • Hi, thank you. In my project I have created two forms. I have my main form and my xml form. When i go to my xml form I just want access to my listview items in my main form to populate my xml combo box.
    Wednesday, June 24, 2009 6:27 PM
  • Hi Otis,

    From my experience, you can define a public function in xml form and call that function in main form to populate the combo box. This is the code snippet: 

    Main form:
    private void MainForm_Load(object sender, EventArgs e)

    {

        //initial the list view.

        this.listView1.Items.Add("01");

        this.listView1.Items.Add("02");

     

        //Initial the xml form and show it.

        XmlForm form = new XmlForm();

        form.PopulateComboBox(this.listView1.Items);

        form.Show();
    }


    Xml Form:
           //Use this method to populate the combo box.

           public void PopulateComboBox(ListView.ListViewItemCollection items)

           {

               foreach (ListViewItem item in items)

               {

                   this.comboBox1.Items.Add(item.Text);

               }
           }


    Let me know if this helps.
    Aland Li

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Edited by Aland Li Wednesday, July 1, 2009 12:05 PM
    • Marked as answer by Aland Li Thursday, July 2, 2009 11:28 AM
    Friday, June 26, 2009 8:43 AM