?
Solved

dynamically adding controls to a winform

Posted on 2010-08-20
3
Medium Priority
?
1,043 Views
Last Modified: 2012-05-10
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
Comment
Question by:ieg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33487702

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
 
LVL 16

Accepted Solution

by:
kris_per earned 2000 total points
ID: 33487720

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
 

Author Closing Comment

by:ieg
ID: 33489201
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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