WPF [SOLVED]: changing multiple textboxes at once

WPF [SOLVED]: changing multiple textboxes at once

Home Forums Frameworks WPF WPF [SOLVED]: changing multiple textboxes at once

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246842

    Cloudy Point
    Keymaster

    QuestionQuestion

    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";
        }
    

    #246843

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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 = "need reputation";
                }
    

    Source: https://stackoverflow.com/questions/47933248/changing-multiple-textboxes-at-once
    Author: Yollo
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.