2

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
  • No time to go into any detail now, so comment instead of answer. They way you make every Panel (or UserControl as suggested by CurtisHx) able to receive information from the main form, and/or to add menu items to the main form is to turn it onto its head: make all panels/UserControls implement an interface that allows the mainform to ask each one for any menu items it wants to add and allows the mainform to "broadcast" information from other panels to anyone interested. The latter is the published-subscriber pattern: every panel automatically becomes a subscriber of the mainform. Apr 27, 2015 at 14:03

1 Answer 1

2

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 UserControls. These are kind of like Windows, 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
  • Any nice idea on how to add menu items to the form from the userControl?
    – David
    Apr 15, 2015 at 20:06
  • When the user control gets focus (or some other trigger), the user control can raise a custom event and pass a data structure representing the menu items up to the parent. The user control will tell the main form what to place into the menu.
    – CurtisHx
    Apr 15, 2015 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.