.NET
  Home arrow .NET arrow Dynamically Adding Controls to a Windows F...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Dynamically Adding Controls to a Windows Form
By: W. Daniel Skousen
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 40
    2004-05-26

    Table of Contents:
  • Dynamically Adding Controls to a Windows Form
  • Defining the Attribute File
  • What Do I Need to Store?
  • Creating the Controls
  • Handling the Mouse Up Event

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    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:

    1. 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.
    2. 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.
    3. 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.)

    More .NET Articles
    More By W. Daniel Skousen


       · a great article.One issue i am facing in running your code is that it gives me...
     

    .NET ARTICLES

    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...
    - Delving Deeper into Serialization with .NET
    - Serialization with .NET





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway