-4

In my program i have 50 text boxes with the name TB1 to TB50

I am trying to use a for loop to change the content of them all with a few lines of code. Instead of repeating the code 50 times. However i cant get this to work. Any help would be greatly appreciated (Please see what where i got ot below)

private void Button_Click(object sender, RoutedEventArgs e)
{
    for (int i =1; i < 50; i++)
    {
        string TB = "TB" + i;

        TextBox TBN = new TextBox();

        TBN.Name = TB;
        TBN.Text = "Textbox Changed";
    }
  • 1
    You should make an array. – SLaks Dec 21 '17 at 21:56
  • 4
    You're creating new text boxes, not using the ones on your form/window. – Broots Waymb Dec 21 '17 at 21:57
  • But how do i set the textbox name to be TB? as currently it cant convert the string to textbox? – ben edwards Dec 21 '17 at 21:58
  • If you've created the textboxes in the designer, why don't you change the names of them there? – itsme86 Dec 21 '17 at 21:59
  • 2
    Take a step back. You do not want to be creating new text boxes in here in order to change the text of existing ones. That just does not make sense. You need to find the ones that already exist. – Broots Waymb Dec 21 '17 at 22:00
  • 1
    `this.Controls[TB].Text = "Text Changed";` Assumes controls are not in a panel, etc. Use `this.Controls.ContainsKey(TB)` to make sure the control name exists. That being said, use a grid. – LarsTech Dec 21 '17 at 22:01
  • 3
    My sympathies to your users – Ňɏssa Pøngjǣrdenlarp Dec 21 '17 at 22:02
  • You can create and manage an array of 'Controls', but its a bit intense I think http://www.dreamincode.net/forums/topic/45666-control-arrays-in-c%23/ – Grantly Dec 21 '17 at 22:02
  • You could loop through the controls on the form, or panel, or whatever you have your text boxes on, check that the control is a text box, and then update its text. If you have other text boxes on the form/panel, check that the name matches your criteria like @LarsTech alluded to. – Phil N DeBlanc Dec 21 '17 at 22:03
  • I've read your statement "currently it can't convert the string to textbox" several times and I can't for the life of me figure out what you're talking about. What is "it"? Which string are you talking about? Are you saying you're trying to convert a string type to a TextBox type? Why would you think that would work at any time let alone currently? – itsme86 Dec 21 '17 at 22:03
  • 2
    Hmm, RoutedEvents... that makes this WPF? – LarsTech Dec 21 '17 at 22:11
  • Possible duplicate of [Loop through Textboxes](https://stackoverflow.com/questions/4863051/loop-through-textboxes) – Ken White Dec 21 '17 at 22:21
  • You need a collection bound to an ItemsControl. Do you have any viewmodel at all? – 15ee8f99-57ff-4f92-890c-b56153 Dec 21 '17 at 23:21

2 Answers2

0

Try this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    for (int i =1; i < 50; i++)
    {
        string TB = "TB" + i;

        // credit to sTrenat
        TextBox TBN = this.Controls[TB];

        TBN.Text = "Textbox Changed";

    }
user2908532
  • 21
  • 1
  • 4
0

first get all textboxes of some element depending where your textboxes are, for example if they are in gird just replace rootControl with name of your grid, after that it's lemon squeezy, using this approach textbox name doesn't matter.

IEnumerable<TextBox> TextboxCollection = rootControl.Children.OfType<TextBox>();
            foreach(TextBox tb in TextboxCollection)
            {
                tb.Text = "Text";
            }
Djordje
  • 437
  • 1
  • 12
  • 24