Organize UI Elements With User Controls
No one wants to create a WinForm with more than a hundred controls. User controls help you encapsulate your code.
by Bill Wagner
November 2003 Issue
Technology Toolbox: VB.NET, C#
Microsoft touts user controls in WinForms as a way to package reusable components for Windows user interfaces. But did you know user controls can help you organize similar functionality?
A user control is a Windows control that functions as a container. You can place any WinForms controls on a user control, just like you can place them on a standard Windows form. The user control contains all the working code with any of the controls you place in it.
Visually, there's no difference between using a user control and placing other controls directly on a form. The difference is in the code. When you use a user control, you place all the code relating to those controls in the user control class. Otherwise, all the code that works with every control is in the form class. This use of a user control can improve your code's organization.
Organize Your Tab Pages
I recently created a main window with 10 different tabs that each contained between 10 and 30 different controls, including all the labels. I used the standard VS.NET wizards to make every tab page and every control a member of the form class, which put more than 300 controls in that one form (see Figure 1). The code quickly became unwieldy. Even with all the tools in VS.NET to browse and search, there were simply too many variables and event handlers for me to work efficiently.
Back in the days of native code, MFC created separate classes for each page in a tabbed window, making the code more encapsulated and easy to handle. I could work with each of the pages independently, and I didn't worry about the code on other tab pages.
You can use .NET to get the same encapsulation you could achieve with the old MFC library. All you need to do is create one user control for each page, and you'll be able to organize your code better.
The form contains a collection of tab pages, and each page contains one user control (see Figure 2). The user controls contain all the Windows controls for that page. You can work on the user control and not be distracted by all the controls and elements that don't apply to the current task.
Step by Step
First, create the form, and use VS.NET's Add… menu to create a new user control. Next, add child controls to the user control as needed, then build the project.
Your new user control shows up in the VS.NET toolbox when you load the current project. Now you can drag and drop the user control to any of the tab pages to place the user control on the form. The only drawback to this strategy is you need to build the project in order to see any changes to the user control in the main window's designer. But all in all, this organization makes it much easier to manage code that should be distinctly separate.
About the Author
Bill Wagner has been an independent software consultant for more than nine years and cofounded SRT Solutions in 2001. He's a nationally recognized expert on Microsoft .NET whose strengths are in object-oriented design, .NET development, graphics algorithms, and data analysis. His technical skills include development in .NET, C#, C++, ASP.NET, and ASP. He published C# Core Language Little Black Book (Coriolis) in 2001, and he's a regular contributor to Visual Studio Magazine and the .NET Insight newsletter. Reach him at . Back to top
|