• 0

Using strings to control text boxes?


Question

So I am loading an XML file to get all the settings for a program. Not a problem, I've got what I need regarding that. However, once the data is loaded, my goal is to load the data into text boxes (to show the current settings) so that the user can edit the settings as necessary... and then they'd be able to write the settings back to the XML file.

For example... I have:

Textbox: txtWidth

var1 = "txtWidth"

var2 = "800"

I want to make it so in effect it's this: var1.text = var2

I have close to 100 paramaters that need to be loaded so obviously it's being done through a loop. I just can't figure out how to do this -- if it's even possible.

Does anybody have any ideas?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I'd probably dynamically generate the form based on your data, naming your controls appropriately along the way. FindControl is doable, but time consuming if you call it over and over and over as it is a linear search, and could quite possibly need to be recursive.

You might want to look at data binding and serialization, as well.

Link to comment
Share on other sites

  • 0

hmm... so it would be something along the lines of:

				Control myControl = new Control();
				foreach (Control c in this.Controls)
					if (c.Name == var1)
					{
						c.Text = var2;
					}

?

Link to comment
Share on other sites

  • 0

ahh... got it. thanks guys!

	   private void ProcessControls(Control ctrlContainer, string name, string value)
		{
			foreach (Control ctrl in ctrlContainer.Controls)
			{
				if (ctrl.Name == name)
				{
					ctrl.Text = value;
				}

				if (ctrl.HasChildren)
					ProcessControls(ctrl, name, value);
			}
		}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.