Better method than mdi parent/child forms

AlexJames

Well-known member
Joined
Mar 20, 2020
Messages
65
Programming Experience
10+
Hi All

I'm relatively new to C# and 90% of my experience is in the older languages. I'm busy with a new WinForms C# project which simply has a docked panel on the main form with a bank of buttons to mimic a menu, when one of these menu buttons is clicked I just need to display a new form but with my left menu still visible and clickable. I was playing around with setting up the main form as the mdi parent and the forms to be shown as the child forms. This seems to work ok, but i have quite a bit of UI design and the child forms don't quite display as i would like. So my question really is, is there a better we to achieve this without using mdi parent and child forms ? i also believe that this method is out dated.

Many thanks.
AJ
 
MDI can still work for the right application but many applications have gone towards creating a separate window for each document. In your case, are you actually displaying multiple child windows at the same time? If not, you don;t need MDI or even separate forms. You can use a user control for each of the subsets of functionality and then you create, destroy, show and hide them as you need to.
 
And there is also the Windows UI Guidelines to keep mind of:
 
MDI can still work for the right application but many applications have gone towards creating a separate window for each document. In your case, are you actually displaying multiple child windows at the same time? If not, you don;t need MDI or even separate forms. You can use a user control for each of the subsets of functionality and then you create, destroy, show and hide them as you need to.

Hi Jm

Thank you for your reply, i will only be showing one form at a time so there's no need to show multiple child forms at the same time. I'll have to do some research on "You can use a user control for each of the subsets of functionality and then you create, destroy, show and hide them as you need to. " as i have never done anything like that. Each form will have different controls and functions etc. Would these user controls be forms ?
 
Would these user controls be forms ?
You add a user control to your project like you add any other item, including forms. You open the Add Item dialogue and select the user control item template. If you right-click your project in the Solution Explorer, there is a dedicated menu item. Just like all the forms you add inherit the Form class, so all user controls inherit the UserControl class. You design a user control in exactly the same way as you design a form, then you use it in exactly the same way as you use any other control. It will even be added to the Toolbox automatically when you build. Note that, ideally, you would make all the child controls private and then just expose what you specifically need, e.g. you might add a String property to get and set the Text of a child TextBox. You also need to add appropriate events, e.g. you might internally handle the Click event of a Button and then raise your own ButtonClick event.
 
You add a user control to your project like you add any other item, including forms. You open the Add Item dialogue and select the user control item template. If you right-click your project in the Solution Explorer, there is a dedicated menu item. Just like all the forms you add inherit the Form class, so all user controls inherit the UserControl class. You design a user control in exactly the same way as you design a form, then you use it in exactly the same way as you use any other control. It will even be added to the Toolbox automatically when you build. Note that, ideally, you would make all the child controls private and then just expose what you specifically need, e.g. you might add a String property to get and set the Text of a child TextBox. You also need to add appropriate events, e.g. you might internally handle the Click event of a Button and then raise your own ButtonClick event.

Thank you JM
I've found and added the UserControl to my project, now i have an idea on what to research. I'm sure i'll post back with more questions, as this is brand new territory for me lol.
 
Actually, there's a few options here:
As originally suggested, you could dynamically show and hide controls that are already in the window, or dynamically add and remove controls. That's typically how a lot of modern web based SPA's (single page app) work.

The much simpler alternative is simply use modal dialogs for those other forms that you pop up, collect information from the user, and then dismiss the dialog. This may feel like a throwback to the 1980's before MDI applications became the rage (mostly because of Apple Macintosh UI).

Another question is if the user has to provide information in some sequential manner. If so, then the 1990's style wizard would likely be a better UI, particularly if the goal of the program is to just produce a set of results and there is no real need to keep going back to tweak the parameters that generated the results.

I highly recommend stepping back from the problem a little bit and think about what the purpose of the program is, and what would be the ideal workflow to accomplish that. Don't lock yourself in yet into the idea of menus, toolbars, wizards, dialogs, etc... You may end up discovering that all that is really needed is a single free form page where the user drags and drops "items" from a palette unto the surface and connects things together ala Visio. Or you may discover that you ask the user just 1 or 2 initial questions on start up and you generate an initial result, then a user just needs to right click and choose from a small set of predefined actions -- and right clicking maybe more natural than hitting toolbar buttons.
 
Thank you both for all your input, they have really helped me with researching and how best to get the app to work the way i need. I have a total of about 10 forms that require different data from users, so what i've done to move away from the mdi parent/ child, is to add a Panel onto my main form and set the dock property to fill, when i show my new forms i add them into the container with the below code, and this seems to work really well without strange things happening with the UI as it was with mdi.

C#:
                    Form frm = new FrmSetupExchanges();
                    frm.TopLevel = false;
                    frm.ControlBox = false;
                    frm.Dock = DockStyle.Fill;
                    PnlContainer.Controls.Add(frm);
                    frm.Show();

Do you guys foresee any problems going forward using this method ?
 
Although it may seem to work now, stuffing a Form as a child window may lead to unexpected behavior in the future. It would be better to make FrmSetupExchanges into a UserControl. This has the added benefit of not needing to call Show().

If you are using the WinForms Designer, designing a user control is no more different than designing a form. Just drag controls into the design area and setup properties and events.
 
Thank you for the feedback Skydiver, so just to clarify.
1. I would need a new user control for all 10 "forms" ?
2. i can design the user controls the same way as a normal form, only difference is that i need to code the events for my controls ?
3. Do i display the user control in the main forms panel ? or do i set the users dock property to fill ?

many thanks again Skydiver for all your input.
 
Hi Guys

I've been experimenting with the use control, and here's what i have so far.
I have removed the Panel from my main form (i was using this previously to show my new form in), I have added and designed a User Control form and am calling it with the below code.

C#:
                    UcDeposits uc = new UcDeposits();
                    Controls.Add(uc);
                    uc.Dock = DockStyle.Fill;
                    uc.BringToFront();

The only issue i seem to be experiencing now is that when the user control is shown it first fills the entirety of the main form and then resizes to fit correctly, so i get this horrible flash of screen before it fits correctly. Should i still be using a docked panel on my main form to put the user control form in ? or am missing a step somewhere ?

many thanks again for both of your input, i've learned a lot this week.
 
If you are doing that in your "Load" or "Shown" event, or any point after initially loading the form, yes, you will see a lot of the redrawing that happens. Ideally, you should do all that in the constructor before anything is displayed, but if you really must delay things until later because of dynamically adding and removing controls, use the SuspendLayout() and ResumeLayout() methods to stop the redrawing until you are done adding/removing controls. It may also help to design your user controls to the ideal size that you will be using with your form.
 
It may also help to set Dock property before adding the control to avoid layout twice.
 
I would recommend using an object initialiser like so:
C#:
var uc = new UcDeposits {Dock = DockStyle.Fill};
Using the object initialiser like that ensures that the setting of properties is done immediately after construction. You can set as many properties as you like that way, separated by commas.
 
Last edited:
Back
Top Bottom