简体   繁体   中英

How can I properly use methods from a class in another class in C# Visual Studio 2010

So, I have a Form2 with the method public void LoadTree() (which loads data from a database onto a TreeView in Form2), I also have a Form3 that starts on a button click from Form2. In Form3 on a button click (after some code executes) I want to use the LoadTree() method from Form2.

I have the following code in Form3 to do this:

    private void button1_Click(object sender, EventArgs e)
    {
        var loading = new Form2();
        loading.LoadTree();
        loading.Show();
    }

All works and executes well,no errors, but at the end of this I have a Form2, a Form3, and another Form2, If i try to put a code to close the first Form2, the entire program shuts down, what could I do about it?, I just want that after I click the button on Form3, Form2 to show as updated.

Problem : You are creating the newinstance of Form2 (or whichever Form you want to show) everytime, so that it creates a new instance everytime and all your old changes will notbe available.

Solution : Don't create a new instance of Forms which are hidden while switching between the different Forms,rather open the already existing Form2 from Memory using the Application.OpenForms[]

Try This:

Form2 form2 = (Form2) Application.OpenForms["Form2"];

//now use form2 variable
form2.LoadTree();
form2.Show();

I have a Form2 with the method public void LoadTree() (which loads data from a database onto a TreeView in Form2

Do not make database calls directly in a form. Put this code in another class, better yet use n-tier.

In Form3 on a button click (after some code executes) I want to use the LoadTree() method from Form2.

And

I just want that after I click the button on Form3, Form2 to show as updated.

Form3 raises the event which is to be handled by Form2. Best practice is to use events for communicating messages across forms. You could use a property here, but this would be a side effect of a property and thus not a good way to accomplish the goal.

public partial class Form2 : Form
{        

    public Form2()
    {
        InitializeComponent();
    }

    private void LoadTree(Object o, EventArgs e)
    {
        //do work
        MessageBox.Show("Loading tree...");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var form = new Form3();
        form.LoadTreeEvent += form_LoadTreeEvent;//Hook into the Form3 event 
        form.Show();
    }

    void form_LoadTreeEvent(object o, EventArgs e)
    {
        LoadTree(o, e);//Handle Form3 Event
    }
}

public partial class Form3 : Form
{
    //The event is raised by Form3, however it's handled by Form2.
    public event LoadTreeHandler LoadTreeEvent;        
    public delegate void LoadTreeHandler(Object o, EventArgs e);

    public Form3()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LoadTreeEvent(null, null);//Raise the event in Form3 and pass whatever
        //do work...maybe close this form??
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM