Adrian Collado
Adrian Collado

Reputation: 709

c# dynamic control name

I am trying to create controls at runtime by using an XML file. How would I access these controls after creating them with names specified by the XML file?

For instance, if I had a series of controls in an XML file:

<control id="0" type="textbox">
   <param id="0" type="(name)">DynamicTextbox</param>
   <param id="1" type="location">100,73</param>
   <param id="2" type="size">119,20</param>
</control>
<control id="1" type="button">
   <param id="0" type="(name)">DynamicButton</param>
   <param id="1" type="location">200,82</param>
   <param id="2" type="size">78,50</param>
</control>

If I designed the UI in design mode, and I didn't use the XML file, I could simply call the controls with their respective names.

DynamicTextbox.location = new Point(100,73);
DynamicTextbox.size = new Size(119,20);

DynamicButton.location = new Point(200,82);
DynamicButton.size = new Size(78,50);

Since I am creating the controls at runtime, how would I access them with those names?

Thanks!

Upvotes: 1

Views: 1561

Answers (2)

musefan
musefan

Reputation: 48425

I take it you are adding the control to a UI element? YOu should be able to access them through that. So in WinForms you might have something like...

TextBox tb = new TextBox();
tb.Name = "DynamicTextbox";
Panel1.Controls.Add(tb);

TextBox tb = (TextBox)Panel1.Controls["DynamicTextbox"];

If this doesn't help then let us know what type of application you are working with (WinForms, WPF, ASP.NET etc.)

Upvotes: 1

GreyCloud
GreyCloud

Reputation: 3100

Think about how you would tell one button appart from another? I think you will need to put a name field into your XML and then set the buttons name to whats in the XML when you create the button.

Another option is to use reflection to find ALL the buttons or ALL the TextBoxes but again if you dont have any way to uniquely identify them in the xml then you wont be able to tell them appart at run time.

Upvotes: 0

Related Questions