Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Dynamically Loaded Windows Form

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.
0
rufassa
Asked:
rufassa
  • 2
1 Solution
 
rufassaAuthor Commented:
I have figured out how to create the subform. So I only need an answer to the first question.
0
 
sourabhkothariCommented:
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;
doc.load(filename);
xmlnode root = doc.documentelement;

now access elements using root node.

Skot
0
 
RosettaStoneCommented:
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();
                  ds2.ReadXml(strM,XmlReadMode.Auto);
                  
                  tb2 = ds2.Tables[0];
                  return tb2;
            }
            catch (Exception e) {
                  Console.WriteLine(e.ToString());
                  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;
                  panel.Controls.Clear();                                    
                  
                  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;
      
                              pnlFrm.Controls.Add(lblTmp);
                              
                              //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;
                              pnlFrm.Controls.Add(ctrlTmp);
                  
                        } //a if for every type you want the process is alike, you mike just want to check
                          //the properties names of the other controls
                  }
                        
            }
0
 
RosettaStoneCommented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now