Communicate between two windows forms in C#

后端 未结 12 1760
温柔的废话
温柔的废话 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:35

    A form is a class, just like any other class. Add some public variables to your form class and set them when they click the button to close the form (technically they are just hiding it).

    A VB.NET example, but you'll get the idea -

    In your OptionsForm class:

    Public Option1 as String = ""
    

    etc. Set them when they hit the "Ok" button.

    So in your main form, when they hit the "options" button - you create your options form:

    OptionsForm.ShowDialog()
    

    when it exits, you harvest your option settings from the public variables on the form:

    option1 = OptionsForm.Option1
    

    etc.

提交回复
热议问题