Solved

dynamically adding controls to a winform

Posted on 2010-08-20
3
1,033 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
  • 2
3 Comments
 
LVL 16

Expert Comment

by:kris_per
Comment Utility

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 500 total points
Comment Utility

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
Comment Utility
Many thanks - you were spot on with the solution.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

14 Experts available now in Live!

Get 1:1 Help Now