Communicate between two windows forms in C#

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

    Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. This solves the communication problem. For example I've exposed Label Property as public in Form1 which is modified in Form2.

    With this approach you can do communication in different ways.

    Download Link for Sample Project

    //Your Form1

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.Show();
        }
    
        public string LabelText
        {
            get { return Lbl.Text; }
            set { Lbl.Text = value; }
        }
    }
    

    //Your Form2

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    
        private Form1 mainForm = null;
        public Form2(Form callingForm)
        {
            mainForm = callingForm as Form1; 
            InitializeComponent();
        }
    
        private void Form2_Load(object sender, EventArgs e)
        {
    
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            this.mainForm.LabelText = txtMessage.Text;
        }
    }
    


    (source: ruchitsurati.net)


    (source: ruchitsurati.net)

    0 讨论(0)
  • 2020-11-21 05:35

    The best in this case would be to have some OptionsService class/interface that is accessible via IServiceProvider.

    Just add an event when something changes, and the rest of the app can respond to it.

    0 讨论(0)
  • 2020-11-21 05:35

    You might try AutoMapper. Keep your options in a separate class and then use AutoMapper to shuttle the data between the class and the form.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:39

    Create a Class and put all your properties inside the class .. Create a Property in the parent class and set it from your child (options) form

    0 讨论(0)
  • 2020-11-21 05:39

    MVC, MVP, MVVM -- slight overkill for someone admittedly saying they want tutorials. Those are theories that have entire courses dedicated to them.

    As already posted, passing an object around is probably easiest. If treating a class as an object (interchangeable in this sense) is new, then you may want to spend another 2-4 weeks figuring out properties and constructors and such.

    I'm not a C# master by any means, but these concepts need to be pretty concrete if you want to go much further beyond passing values between two forms (also classes/objects in their own right). Not trying to be mean here at all, it just sounds like you're moving from something like VB6 (or any language with globals) to something far more structured.

    Eventually, it will click.

    0 讨论(0)
提交回复
热议问题