.NET
  Home arrow .NET arrow Page 5 - Dynamically Adding Controls to a Windows F...
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 
Mobile Linux 
App Generation ROI 
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
     
     
    ADVERTISEMENT


    Dynamically Adding Controls to a Windows Form - Handling the Mouse Up Event


    (Page 5 of 5 )

    The Mouse Up event handler does not contain many surprises. Here is how it looks:

    private void cbName_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {

     // Cast the sender object to the check box
     // that received the mouse up event.
     CheckBox cbName = (CheckBox) sender;

     // Locate the associated numeric up down from our hash table of controls.
     // The key is the text of the check box (stored previously)
     NumericUpDown nudName = (NumericUpDown) nudControls[cbName.Text];
     // Get out the current value
     int val = (int) nudName.Value;

     // Figure out which button they clicked.
     if (e.Button == MouseButtons.Left) {
      val++;
     } else if (e.Button == MouseButtons.Right) {
      val--;
      if (val < 0) {
       val = 0;
      }
     }
     // Set the value of the numeric up down
     nudName.Value = val;

     // Check to see if we should have the checkbox checked
     if (val == 0) {
      cbName.Checked = false;
     } else {
      cbName.Checked = true;
     }
    }

    Again, the idea here is that we want to increase the value of the associated numeric up down if the left mouse was clicked, and decrease the value if the right button was clicked.
     
    First, we know what specific checkbox we are dealing with because the method passes in the sender object, in this case the desired check box. We immediately cast it to a CheckBox object so we can access and modify some of its parameters.

    Next, the trick is getting the appropriate numeric up down control. Recall that when we created the control, we added it to an internal hash table called nudControls, using the name of the medication as a key. This also happens to be the Text property of the checkbox. So we can easily locate the numeric up down control that we need by asking for it by the value of the checkbox Text parameter.

    We then perform the appropriate logic, and our method is complete!

    Resizing the Form

    Last, I thought it would be nice to include a method that would change the size (height) of the form. Since I would not know how many controls I would be adding at design time, I knew that if I wanted all of the controls to be visible to the end-user I would have to handle this dynamically. Here is the method:

     private void SizeToFit() {
       // If called after the dynamic controls were created,
       // vertPos is now the last vertical position of the
       // last item added. Resize the form, adding some bottom spacing.
       Size size = new Size(this.Size.Width, vertPos + 27);
       this.ClientSize = size;
      }

    No surprises, right? I call this method from the constructor, immediately after loading the dynamic controls and calling InitializeComponent.

    Summary

    And there you have it! Not only did I find that this saved me a great deal of work, but deployment became simple. Likewise, future maintenance and modification was a snap. All I had to do was change an XML file, and the next time the form was loaded, all of the items would be placed in their appropriate location, with the appropriately sized dialog. Simple and effective, my favorite kind of software.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT