Best way to communicate between forms?

I almost never used (advanced or general) GUIs or one simple form with simple controls ... but this time I have something more complex and I don’t have much experience with the GUI, I have one main form (and possibly more in the future) from which other subforms open (and they may have subforms), and I wonder what, in your opinion, is the best way to communicate between them?

I thought about passing the main form as a parameter to the subform constructors, but this doesn’t seem like a good way, especially if I need to communicate between other, individual forms, not to mention the fact that I have to double-check the input or do several methods, but this is more like functional programming than object-oriented programming ...

Maybe I can:

  • Create a static class (or Properties.Settings) for global settings. Cons: every data change needs to be copied to the class, I'm looking for something more convenient and elegant.
  • Use the ugly way to access controls from Application.OpenForms - fixes the problem of passing the main form as a parameter. Cons: not very stable.
  • Do something else that I didn’t think about. Suggestions? Cons: I do not know what it is.
+3
source share
4 answers

The idea of ​​your constructor is probably the most reliable way to communicate with the main form. Your subformation will do something like this:

public class SubForm : Form
{
    public SubForm(MainForm parentForm)
    {
        _parentForm = parentForm;
    }

    private MainForm _parentForm;

    private void btn_UpdateClientName_Click(object sender, EventArgs e)
    {
        _parentForm.UpdateClientName(txt_ClientName.Text);
    }
}

And then you publish the public methods on your MainForm:

public class MainForm : Form
{
    public void UpdateClientName(string clientName)
    {
        txt_MainClientName.Text = clientName;
    }
}

Alternatively, you can go differently and subscribe to events from your subformats:

public class MainForm : Form
{
    private SubForm1 _subForm1;
    private SubForm2 _subForm2;

    public MainForm()
    {
        _subForm1 = new SubForm1();
        _subForm2 = new SubForm2();

        _subForm1.ClientUpdated += new EventHandler(_subForm1_ClientUpdated);
        _subForm2.ClientUpdated += new EventHandler(_subForm2_ProductUpdated);
    }

    private void _subForm1_ClientUpdated(object sender, EventArgs e)  
    {
        txt_ClientName.Text = _subForm1.ClientName; // Expose a public property
    }

    private void _subForm2_ProductUpdated(object sender, EventArgs e)  
    {
        txt_ProductName.Text = _subForm2.ProductName; // Expose a public property
    }
}
+1
source

A good way is to declare delegates in a form that wants to start sharing data. You need a delegate and a callback function:

public delegate void SetValueDelegate(string value);
public SetValueDelegate SetValueCallback;

. , :

firstForm.SetValueCallback += new SetValueDelegate(secondForm.SetValueFunction);

, :

public void SetValueFunction(string value)
{
    // do something
}

( , :

SetValueCallback(txtParam.Text);

:

using System;

namespace DelegateTest
{
    public delegate void SetValueDelegate(string value);

    public class Class1
    {
        public SetValueDelegate SetValueCallBack;

        public void Test()
        {
            if(SetValueCallBack != null)
            {
                SetValueCallBack("Hello World!");
            }
        }
    }

    public class Class2
    {
        public void SetValueFunction(string value)
        {
            Console.WriteLine(value);
        }
    }

    public class Launcher
    {
        public static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            Class2 c2 = new Class2();
            c1.SetValueCallBack += new SetValueDelegate(c2.SetValueFunction);
            c1.Test();
        }
    }
}
+1

, "".

public Form1() {  (ComplicatedDataStructure) = ComplicatedDataStracture(); } , , form1 = new Form1(); , , form2 = new Form2(); , , form2.Tag = form1.Tag;

form2.Tag "ComplicatedDataStracture";

0

, ( ) CAB (Composite Application Block). , CAB 2-3 , , UI . ( ).

: , .

( ) , .

0

Source: https://habr.com/ru/post/1780195/


All Articles