2-way communication with Winforms

Today one of my colleagues asked me if it was possible for 2 Windows Forms to communicate with each other in .Net 3.5

Of course it is, and it is easy too.
So I made him an example that showed an easy way to do it.

I’ll try and explain it.

This is what the forms look like:

These 2 images already show that I sent ‘hello’ from Form1 to Form2 and that I let form1 know that I didn’t much care for it.

It’s really easy. I’ve given Form1 a private field with the Form2 in it

private Form2 commform = new Form2();

This of course allows us to access Form2 in every function.

Now when we click on the Send button, this function is executed:

private void sendButton_Click(object sender, EventArgs e)
{
    commform.Settext(sendTextBox.Text);
}

This is, of course, just a regular event handler. It calls Form2’s (named commform, as seen above) Settext() function. Which only takes a string argument.

public void Settext(string text)
{
    messageTextBox.Text = text;
}

That function only sets the text for messageBox. Easy.

Now the other way around we could of course give Form2 a private field container Form1 and then fill that variable when our program starts, but I didn’t do it that way here.
Any dialog box or form in WinForms can have an Owner specified. So in the On_Load function I used the Form’s overloaded Show(IWin32Window owner) method to show the window and set the owner, like this:

private void Form1_Load(object sender, EventArgs e)
{
    commform.Show(this);
}

That allows us to do this in our like and hate button event handlers:

private void likeButton_Click(object sender, EventArgs e)
{
    ((Form1) Owner).Like(true);
}
 
private void hateButton_Click(object sender, EventArgs e)
{
    ((Form1) Owner).Like(false);
}

We cast the Owner property to a Form1, because right now we know that the owner will always be a Form1, and call it’s Like() function which takes a boolean indicating whether or not you liked it. That function looks like:

public void Like(bool like)
{
    if (like)
    {
        responseLabel.Text = "You liked it";
        responseLabel.ForeColor = Color.Green;
    }
    else
    {
        responseLabel.Text = "You hated it";
        responseLabel.ForeColor = Color.Red;
    }
}

Which checks the boolean parameter, if it is true (you liked it), it sets a label’s Text to “You liked it” and the text color to green, otherwise (it’s false, you didn’t like it) it sets the Text to “You hated it” and the text color to red.

That’s just about it… Like I said, easy.
Though of course I did keep it simple, if another form (say Form3) would try to do open Form2 and you’d click like or dislike then it’d crash and burn. There’s many ways to handle such things, but those can get less-easy than this.

You can look at/download the complete source and project file for this example here, they may offer some extra insight since I’m just showing bit of code from one and the other in no particular order. If you do choose to download the source code (top right), you will also be downloading any other csharp examples I put there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s