I have started to develop a support tool for an old legacy system that is no longer maintained but still needed for the foreseeable future. The legacy system offers few, clunky and error prone methods for basic tasks such as handling users, rules, data imports and settings. The Tool I am developing will be used by the inhouse support team.
I’m using panels to group related tasks together. I intend to load each panel into a TabControl on user request. The main form have a few user settings that affects/ all panels. A panel might perform a task that other panels need to aware of.
Question TL:DR
How can the MainForm
be made aware that Panel
A have made a significant change that other panels
might be interested in?
How can a panel receive information from MainForm
?
My thoughts so far:
The MainForm
could get information from each Panel
by subscribing to custom events (such as a UsersUpdatedEvent
). But how to do the other way around since a Panel does not “know” the parent form?
Use example:
Homer wants to add a user and a new rule for company “Duff Beer”. He opens the application and selects “Duff beer” from a dropdown list. He then clicks on a “User” toolstrip icon and the user panel opens in a tab. Homer creates user “Lisa” and then switches over to “Rules” panel and creates a new rule that is owned by Lisa.
Bonus question:
I would also like each panel
to be able to add menu items to the MainForm
Menustrip
. Now that I think about it, the application is a bit like a very very lightweight Visual studio. With tabs and menus and toolbars that change depending on whtat the user is watching.
1
First off, don’t use Panels to group related tasks. Use UserControls
. I used panels to group controls on my first WinForms application, and it turned into a horrible monstrosity, worthy of appearing on TDWTF. Just…don’t do it.
Instead, use UserControl
s. These are kind of like Window
s, but are hosted inside of a container, similar to a Button
or ComboBox
. Normal function calls are used for MainForm
to UserControl
communication. Events are used for UserControl
to MainForm
communication.
For example, you might have a UserControl
called CreateNewUser
. It would contain a text box for the new user’s name and a button to create a new user’s name. It would also need a way of notifying the MainForm
that the button was clicked.
The individual UserControls
do not need to know about the MainForm
. All they need to do is provide public (usually custom) events that their parent can then use. CreateNewUser
does not need to care what created it. All CreateNewUser
needs to do is let its parent know when the user has created a new user.
To answer your question..
How can a panel [UserControl] receive information from MainForm?
The panel [UserControl] will provide public functions / methods that the MainForm can call. The code behind the UI in WinForms is very similar to plain old C# classes. In fact, they are. They can have public methods and properties that anyone can call.
Here’s the MSDN for WinForms UserControls: https://msdn.microsoft.com/en-us/library/aa302342.aspx
2