Communicate between two windows forms in C#

后端 未结 12 1835
温柔的废话
温柔的废话 2020-11-21 04:40

I have two forms, one is the main form and the other is an options form. So say for example that the user clicks on my menu on the main form: Tools -> Options

12条回答
  •  青春惊慌失措
    2020-11-21 05:20

    You can have a function in Form B like so:

    public SettingsResults GetNewSettings()
    {
        if(this.ShowDialog() == DialogResult.Ok)
        {
             return new SettingsResult { ... };
        }
        else
        {
             return null;
        }
    }
    

    And you can call it like this:

    ...
    using(var fb = new FormB())
    {
        var s = fb.GetNewSettings();
        ...
        // Notify other parts of the application that settings have changed.
    }
    

提交回复
热议问题