Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I have got 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:

C#
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:

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


Why I am not seeing any results?
Posted
Comments
BillWoodruff 30-Nov-13 9:49am    
To respond to this I'd need to know how the Settings Form is created and displayed. In this application, what is the "main" Form: the Form launched by the Program.cs static Main method ?
Karthik_Mahalingam 30-Nov-13 10:26am    
i tried your code by creating two forms, its working fine..

r u using MDI ?
Exinta Ennea 30-Nov-13 10:54am    
@BillWoodruff:

IMAGE: http://s30.postimg.org/9feuzdve9/screenshot_129.png

Here you can take a look at my Program.cs, it is a bit specific because I have got third form which is supposed to be Login form.

@karthik mahalingam01:

What is MDI?
BillWoodruff 30-Nov-13 21:54pm    
Your Program.cs file looks fine to me, but I don't see any evidence of a 'Settings Form ... just use of a 'LogIn Form if an Application setting, 'AskForPass is 'true. I assume the 'LogIn Form does the right thing if user log-in is successful to update the Application setting.

So, I am no closer to understanding your problem with the 'Setting Form.

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Create an instance of the second form and if the method is a public one, you can call it directly.
 
Share this answer
 
Try do run it in debug mode. Step-by-step. And see what is happening.

From description, your design might be a little faulty. If Main form is start-up form of your application and Setting form is some dialog occasionally shown from Main, it doesn't make much sense to instantiate Main again in Settings just to call a method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900