Dynamically Adding Controls to a Windows Form
(Page 1 of 5 )
For a simple and effective solution to writing an application that can be used by several different clients, look into Skousen's article here on defining attributes needed to create a form dynamically. He reduces the need to compile and to take extra Tylenol.
Support files for this article can be found here.
In my development of various C# applications, I came upon a common issue that most developers find daunting. I’m writing an application that will potentially be used by several similar, but different, clients. I knew each of them would want to have a series of custom items on a page, each one appropriate for their company and business rules. As most developers know, this becomes a maintenance nightmare. A number of potential solutions present themselves:
- Make a different application for each company. Ouch! Not only is that a lot of work, but trying to keep everyone current is a huge bother. I suppose you could alleviate the pain somewhat by storing common functionality in a common code area, but breaking it out of company-specific code is not only is costly in development time, but calculating which pieces should be called for whom lends itself to additional CPU cycles when the application is run.
- Make a different form for each company. Okay. Sure, you can use a factory pattern to pull up the appropriate forms, but do you really want to have every potential company's form in the application? It makes for a monster program, again sacrificing speed and maintenance. A whiz with building scripts could make it so it would only include the appropriate forms for that company, but this adds an additional level of complexity that results in potential breakage and maintenance.
- Define in an external file the attributes needed to create a form dynamically. When you deploy, you only install the file appropriate to that company. The application takes care of the rest, reading in the file and creating the form on the fly. As an added bonus, if a company adds a new item, or needs to take on off, the change is as simple as modifying the attribute file. When the application is re-run, the new attributes are instantly included. No need to compile anything!
I liked the last idea best, especially since it would potentially allow me to train the customer to make their own enhancements. Hey, any simplistic development I can push off my plate so I can focus on the more difficult stuff is a good idea in my book!
Due to the structure and flexibility of .NET, I found it was a snap to implement my idea.
The test application was a simple one: I wanted a form that helped me keep track of all the medication I take while working on a last minute, midnight hour application. (Hey, just kidding here, folks, especially if this is my wife or mother reading this). I wanted to define all the medications and their dosages in an external file, creating the form on the fly.
In addition, I wanted to add the ability to click on a medication with my mouse. If I clicked with the left button, I wanted the count of the number of times to go up. In case I had over clicked with the left button, if I clicked with the right, I wanted the count to go down. This handling of mouse events would allow me to test for the handling of events in my dynamically created form.
Lastly, I wanted the form to automatically size itself to fit all of the items I entered.
Here is how I did it. (See the support files link above.)
Next: Defining the Attribute File >>
More .NET Articles
More By W. Daniel Skousen