Interactions between Forms in C#.Net – C# Programming Example
C#

Interactions between Forms in C#.Net

It can happen when developing an application to need multiple forms.
We will take the example of an agenda, on the main form (frmMain), we have a grid that represents slots spread over a week. The second form (frmRDV) will present the details of an appointment.
We will not dwell on all the controls of these two forms for this tutorial, just think that frmRDV will open when you click on a time slot.

 

THE SHOW METHOD

The “basic” way to display frmRDV is to create an instance of this form and display it with the Show method.

C# Code:

But there is no interaction, each form lives its life independently. How to know in frmRDV what is the start time of the slots, the end time, if there is already an appointment etc … And even once the details created, modified or deleted how frmMain will know if the slot should to be posted busy or not?

THE SHOWDIALOG METHOD

This method makes frmRDV modal, ie the user will have to close this form to access frmMain again.

C# Code:

There is a beginning of interaction, because frmMain is blocked while frmRDV is displayed, but still no data transfer.

INPUTBOX

The InputBox control as it exists in VB6 (for example) does not appear in the .Net controls arsenal. However, it is neither more nor less than a modal form that returns information to another.
Suppose we want to retrieve a string, let’s add a public method of type string to frmRDV, this method will display the form in modal mode and return the content of a textbox once it is closed.
C # code:

We finally have a data transfer, but in one way, of a single type and only at the closing of frmRDV.

With an DetailRDV class, we can return to frmMain all the information needed to register an appointment. However, it is not possible to transfer information in the other direction.

BIDIRECTIONAL INPUTBOX

Since we have converted frmRDV to InputBox using a dedicated method, passing parameters to this method is a way to transfer data from frmMain to frmRDV.
C # Code:

So finally we have an exchange between the two forms, remain two disadvantages:
– The opening of one blocks the execution of the other;
– The return information transfer can only be done at the closing of the second form.

 

PASSING PARAMETERS IN THE FORM BUILDER AND GENERATING EVENTS

If our frmRDV form has a parameter constructor and can generate public events at different stages of the input (eg an event when the start time is selected or changed, the same as the end time and one for the final validation of the other details), then we will be able to pass our parameters to frmRDV at the instantiation of it and subscribe frmMain to the various existing events.
Bidou wrote a source in C # which is a perfect example of this principle.
For the reader who would code in VB, there are translators in free lines (keywords: “convert C # VB.Net” in his favorite search engine). But with a little intellectual gymnastics it’s, I think, within everyone’s reach to understand the code.

BINDING AND PROPERTYCHANGED

I have already discussed notions of Binding here. In the same form it works alone.
Between two forms, the class used must implement INotifyPropertyChanged.
C # code:

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: