How to call function from another form
c#, void
Solution
This worked for me: In your Program class, declare a static instance of Main (The class, that is) called `Form`. Then, at the beginning of the `Main` method, use `Form = new Main();` So now, when starting your app, use `Application.Run(Form);`
public static Main Form;
static void Main() {
Form = new Main();
Application.Run(Form)
}
Now, calling a function from another form is simple.
Program.Form.MasterReset(); //Make sure MasterReset is a public void
Problem
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 Master 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?