• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1052
  • Last Modified:

dynamically adding controls to a winform

I am trying to create a data entry application that has an element of user configuration.
An XML file should allow a user to add controls to a winform.
e.g.
<?xml version="1.0" encoding="utf-8" ?>
<!--Defines controls on a form-->
<Form name="TestForm">
<Control type="TextBox" name="txtAndy">
<Size width="100" height="20"></Size>
<LocationXY>51,30</LocationXY>
</Control>
<Control> ...... etc etc etc
</Form>

I can't find a way of creating the control using the "name" in the XML file.
If a variable ctlname holds the string "txtAndy" what command can I use to create the object called txtAndy?

this.txtbox= new System.Windows.Forms.TextBox();
is what I need to create but I can't work out how to use txtAndy instead of txtbox

ctlname = new System.Windows.Forms.TextBox();
will obviously fail

Hope someone can help

regards
andy





0
ieg
Asked:
ieg
  • 2
1 Solution
 
kris_perCommented:

I think in your xml "name" is not the name of the variable...its the name for the control..you can set the name for a control like:

this.txtbox= new System.Windows.Forms.TextBox();
this.txtbox.Name = "txtAndy";
0
 
kris_perCommented:

Typically after creating the control you will add it the Form's controls collection like:

TextBox txtbox = new System.Windows.Forms.TextBox();
txtbox.Name = "txtAndy";
txtbox.Location = ...
txtbox.Size = ...

this.Controls.Add(txtbox);

0
 
iegAuthor Commented:
Many thanks - you were spot on with the solution.
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