.NET
  Home arrow .NET arrow Page 3 - 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 
Dedicated Servers 
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 - What Do I Need to Store?


    (Page 3 of 5 )

    It was okay that I didn’t have any private variables (beyond the three labels) since I didn’t need to access or manipulate any of the checkbox or textbox controls after they were created. But I realized that if I wanted to handle the mouse click events, changing the values of the numeric controls, I would need to keep around some references to the numeric up down controls. So I created a private Hashtable to store them in. I called it nudControls. Why wouldn’t I also need to store the check box controls? Well, the Mouse Up event handler looks like this:

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

    The given sender object is the object that sent the event, in this case the checkbox control itself. More on this later.

    Upon further analysis, I observed that I would need to store the vertical position of the last control I placed in the form. This would allow me to re-size the form once all of the controls were dynamically added. So I created a private int called vertPos to store that information.

    Loading from XML

    Next I create a private method to read the XML. Since I was only going to read the file, I decided a standard SAX parser would be sufficient. The .NET XmlTextReader class implements the SAX parser, so I was ready to go. Here is what the method looks like:

    private void LoadFromXML () {
     XmlTextReader xmlReader = null;
     try  {
      //Create an instance of the XMLTextReader.
      // Since we will only be reading the file, a SAX parser will do fine
      xmlReader = new XmlTextReader("Medications.xml");

      while (xmlReader.Read() ) {

       if (xmlReader.NodeType == XmlNodeType.Element) {
        // Look for "medication" tags, ignoring all others
        if (xmlReader.Name == "medication") {

         // Get out the "name" and "dosage attributes
         string name = xmlReader.GetAttribute("name");
         string dosage = xmlReader.GetAttribute("dosage");

         // Create a row of dynamic controls
         vertPos = CreateRow(name, dosage);

        } // test for "medication" name
       } // test for element node type

      } // end loop through reading
     } catch (XmlException ex){
      System.Console.Write("An XML exception occurred: n" + ex.ToString());
     } catch (Exception ex){
      System.Console.Write("A general exception occurred: n" +ex.ToString());
     }       
     finally {
      if (xmlReader != null) {
       xmlReader.Close();
      }
     }
    }

    For those familiar with reading  XML files, the code above is familiar. After creating our xmlReader, we loop through looking for element nodes. If the element Name is equal to “medication,” we extract the “name” and “dosage” attributes, and create a row. The loop continues until all elements have been read.

    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

    - 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
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





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