-1

I have a WinForms application with two forms - Form1 and Form2. I have a loop in Form1 that sends emails. I would like to send the status messages from the loop in Form1 to a RichTextBox in Form2. How can I accomplish this? Any feedback is appreciated.

Eis Karlsson
  • 66
  • 1
  • 12
  • Does this answer your question? [Send values from one form to another form](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – nilsK Apr 08 '21 at 07:58
  • Please take a look at the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) and, if possible, [create a minimal, complete, and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) to improve your question and help us to understand your problem. – nilsK Apr 08 '21 at 07:59
  • I am not sure. I would like to send multiple messages from Form1 to Form2 in realtime. – Eis Karlsson Apr 08 '21 at 08:00

1 Answers1

1

The solution depends on the relation between the two forms: does Form1 know about Form2, for instance because Form1 created Form2?

class Form1 : Form
{
    private Form2 Form2 {get; set;}

    private void CreateForm2()
    {
        this.Form2 = new Form2;
    }

    private void SendStatusMessage(StatusMessage message)
    {
        this.Form2.ProcessStatusMessage(message);
    }
}

If Form1 does not know about Form2, but Form2 knows about Form1, then you should change Form1 such, that it sends events whenever a Status message is available.

public class EventStatusMessageArgs : EventArgs
{
    public StatusMessage Message { get; set; }

}

class Form1 : Form
{
    public Event EventHandler<EventStatusMessageArgs> StatusMessageCreated;

    protected virtual void OnStatusMessageCreated(StatusMessage statusMessage)
    {
         var eventHandler = new EventStatusMessageArgs
         {
              SatusMessage = statusMessage;
         };
         this.StatusMessageCreated?.Invoke(this, eventHandler);
    }

    private void CreateStatusMessage(...)
    {
        StatusMessage statusMessage = ...
        this.OnStatusMessageCreated(statusMessage);
    }
}

class Form2 : Form
{
    // Form2 somehow knows about existence Form1
    public Form1 Form1 {get; set;}

    public void OnFormLoading (object sender, ...)
    {
         // subscribe to events from Form1:
         this.Form1.StatusMessageCreated += this.OnForm1CreatedStatusMessage;
    }

    public void OnForm1CreatedStatusMessage(object sender, EventStatusMessageArgs args)
    {
         StatusMessage createdStatusMessage = args.StatusMessage;
         this.ProcessCreatedStatusMessage(createdStatusMessage);
    }
}

Third scenario: Form1 and Form2 don't know about each others existence. Luckily mainForm who created Form1 and Form2 does:

class MainForm : Form
{
    private readonly Form1 form1;
    private readonly Form2 form2;

    public MainForm()
    {
        InitializeComponents();

        this.form1 = new Form1() {...};
        this.form2 = new Form2() {...};

        // make sure that the StatusMessage events from form1 go to form2:
        this.form1.StatusMessageCreated += this.form2.OnForm1CreatedStatusMessage;
    }           
}

By the way: always unsubscribe from the events when the item is Disposed.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116