In my project I have a Settings form and a Main form. I'm trying to call the Main form's MasterReset function from the Setting form, but nothing happens.

The Main form's MasterReset function looks like this:

public void MasterReset()
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to perform master reset? All settings will be set to default.", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dialogResult == DialogResult.Yes)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string phonebook_path = path + "\\Phonebook\\Contacts.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(phonebook_path);
            XmlNode xNode = xDoc.SelectSingleNode("People");
            xNode.InnerXml = "";
            xDoc.Save(phonebook_path);
            listView1.Clear();
            people.Clear();
        }
        else if (dialogResult == DialogResult.No)
        {
            return;
        }
    }

And I'm accessing it from the Settings form like this

private void btn_MasterReset_Click(object sender, EventArgs e)
{
    Main f1 = new Main();
    f1.MasterReset();
}

Why am I not seeing any results?

Recommended Answers

All 3 Replies

You're not seeing results because you're creating a whole different Main form object and calling the method on that one rather than the actual main form that's been shown.

In this case I'd add an event to your Settings form that gets fired when the button is clicked. The Main form will handle that eventand call its own MasterReset. For example:

public class Main : Form
{
    private void buttonOpenSettings_Click(object sender, EventArgs e)
    {
        using (var dlg = new Settings())
        {
            dlg.ResetSettings += Main_ResetSettings;
            dlg.ShowDialog();
        }
    }

    private void Main_ResetSettings(object sender, EventArgs e)
    {
        MasterReset();
    }

    private void MasterReset()
    {
        ...
    }
}

public class Settings : Form
{
    public event EventHandler ResetSettings = (sender, e) => {};

    protected void OnResetSettings()
    {
        ResetSettings(this, EventArgs.Empty);
    }

    private void buttonReset_Click(object sender, EventArgs e)
    {
        OnResetSettings();
    }
}

@deceptikon:

Thanks for your answer. Anyway, I am still not sure where should I exactly put the code above. Actually, should I break it into pieces and then put one piece in my Main form and the other one in my Settings form, or what.

Please, no hard feelings, I am quite new to c#.

Thanks once again.

I made sure to include two classes, Main and Settings to show you where each piece should be. Without more complete code on your part, I can't really help more than that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.