Dynamically Loaded Windows Form

Posted on 2005-04-21
Last Modified: 2010-04-16
I have to create a form that allows the user to interact with a DB. The tricky part is that the DB fields are subject to change at any time. I have an XML file that contains details about the fields in the DB. I need to use the information in the XML file to build a form everytime the form is loaded. The components I need to be able to load are: Label, Listbox, Combobox, Checkbox, Textbox, and Date Selector.

Also, can I make this dynamic form a scrollable subform. If this is possible I don’t have to worry about positioning the components; I could just list them vertically and allow the user to scroll through them on a main form that contains all static data.
Question by:rufassa

    Author Comment

    I have figured out how to create the subform. So I only need an answer to the first question.
    LVL 1

    Expert Comment

    wot do you want actually, to read the xml file or to connect to db or to load a windows form and populate data in it??
    i think what you want is to read the xml file. so here it goes.

    whenever the form is loaded just read the xml file and populate ur contols with the relevant data.
    for reading the file..
    xmldocument doc = new xmldocument;
    xmlnode root = doc.documentelement;

    now access elements using root node.

    LVL 2

    Accepted Solution

    I develop something like that . The I did was simple Transform the XML into a datatable
    and in the form create a panel.
    for each row in the datatable is a control, and some columns have the desired information to create that control

                public static DataTable dtDataTablefromXML(string xmlStr )
                      DataTable tb2 = new DataTable();
                try {      
                      System.IO.StringReader strM = new System.IO.StringReader(xmlStr);
                      DataSet ds2 = new DataSet();
                      tb2 = ds2.Tables[0];
                      return tb2;
                catch (Exception e) {
                      return tb2;

                public static void BuildPdaForm()
                      DataTable dt = dtDataTablefromXML(@"c:\fields.xml");
                      //when creating forms clean panel first
                      //get the reference to the panel in some way
                      Panel panel = this.pnlFrm;
                      System.Windows.Forms.Label lblTmp;
                      Control ctrlTmp;
                      int iLeft = 2 ;
                      int iSpc = 2 ;
                      int iTop = 2 ;
                      int Ac = 0;
                      foreach(DataRow row in dt.Rows) {
                            if (row["ControlType"] == "LABEL") {
                                  lblTmp = new Label();
                                  lblTmp.Name = row["ControlName"].ToString();
                                  lblTmp.Text = row["ControlValue"].ToString();
                                  lblTmp.Width = 120;
                                  lblTmp.Top = iTop;
                                  lblTmp.Left= iLeft;
                                  //this part you calculate as want to displace the controls in the form
                                  //this line would place the controls like in a single column
                                  iTop = iTop + lblTmp.Top + lblTmp.Height + iSpc;
                            } else if (row["ControlType"] == "TEXTBOX") {
                                  ctrlTmp = new System.Windows.Forms.TextBox();                        

                                  ctrlTmp.Name = row["ControlName"].ToString();
                                  ctrlTmp.Text = row["ControlValue"].ToString();
                                  ctrlTmp.Width = 120;
                                  ctrlTmp.Top = iTop;
                                  ctrlTmp.Left = iLeft;
                                  iTop = iTop + lblTmp.Top + lblTmp.Height + iSpc;
                            } //a if for every type you want the process is alike, you mike just want to check
                              //the properties names of the other controls
    LVL 2

    Expert Comment

    Ok there's some english errors and some mistakes in the above comment, but way is explicit, simply have the necessary information available like the control name and whatever other properties you want to set , like field, and create the control
    and add it to a panel in the way you want.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now