.NET
  Home arrow .NET arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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 - Creating the Controls


    (Page 4 of 5 )

    You will note that the above method calls a method named CreateRow. This is where the actual dynamic creation takes place. Here is what the method looks like:

      private int CreateRow(string name, string dosage) {

       // Calculate the vertical position of the controls.
       // Do this by multiplying the number of items already added
       // (and stored in nudControls)
       // by the height of each item, plus 32 for the spacing at the top
       int yPos = (nudControls.Count * 24) + 32;

       // Calculate the tab order.
       // Do this by multiplying the number of items already added
       // (and stored in nudControls)
       // by the number of controls we are adding on each row
       int tabIndex = (nudControls.Count * 3);

       // Create three form controls, a check box (cbName),
       // a text box (txtDosage), and a numeric up down (nudNum).

       // cbName
       CheckBox cbName = new CheckBox();
       cbName.Location = new System.Drawing.Point(8, yPos);
       cbName.Name = "cbName" + name;
       cbName.Size = new System.Drawing.Size(144, 22);
       cbName.TabIndex = tabIndex++;
       // Note: Here we not only set the text of the item by setting it
       //  to the given name, storing this here will allow us to
       // retrieve it on a mouse up event.
       cbName.Text = name;
       cbName.MouseUp += new
        System.Windows.Forms.MouseEventHandler(this.cbName_MouseUp);

       // txtDosage
       TextBox txtDosage = new TextBox();
       txtDosage.Location = new System.Drawing.Point(160, yPos);
       txtDosage.Name = "txtDosage" + name;
       txtDosage.Size = new System.Drawing.Size(48, 22);
       txtDosage.TabIndex = tabIndex++;
       txtDosage.Text = dosage;

       // nudNum
       NumericUpDown nudNum = new NumericUpDown();
       nudNum.Location = new System.Drawing.Point(224, yPos);
       nudNum.Name = "nudNum" + name;
       nudNum.Size = new System.Drawing.Size(40, 22);
       nudNum.TabIndex = tabIndex++;

       // Add the new controls to this form
       this.Controls.Add(cbName);
       this.Controls.Add(nudNum);
       this.Controls.Add(txtDosage);

       // Add the numeric up down to our internal list so we
       // can later retrieve it. The key is the name of the row.
       nudControls.Add(name, nudNum);

       return yPos;
      }

    Again, the code is pretty self-explanatory.
     
    A few notes: The controls are added to the form using the this.Controls.Add method (this is the same call used for all controls in the form, and can be found in the InitializeComponents method Visual Studio creates for you). 

    Also, observe that we store the NumericUpDown control for later retrieval in our hash table, nudControls. The key for the control value in the hash table is the name of the medication. Since all of my medications can be uniquely identified by the name, this makes sense. But since the hash table complains with an exception if you try to add an item with an identical key, if you have a situation where we tried to add medications with the same name (and perhaps different dosages), we would not be successful. You may need to modify the code and XML to have a unique ID used to store the values.

    Regardless, note that I also set the checkbox Text property to the name of the medication. This comes in handy later when we handle the Mouse Up event. Note in the checkbox creation that adding an event handle is as simple as calling the following, passing in the method that will be called when the event takes place:

     cbName.MouseUp += new
      System.Windows.Forms.MouseEventHandler(this.cbName_MouseUp);

    Lastly, I return the calculated yPos variable so I can store it (in LoadFromXML) for a final resizing of the form.

    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

    - 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 5 hosted by Hostway
    Stay green...Green IT