How to iterate all textboxes on current page
Asked Answered
C

3

6

say I have added many textboxes. How to iterate or loop thru all the textboxes and do some checking. Check if each textbox's content is a number.

Below is the code for winForm, how to do in in WinRT?

foreach (Control item in GroupBox1.Controls)
{

    if (item.GetType() == typeof(TextBox))
    {
        if (string.IsNullOrEmpty( ((TextBox)item).Text))
        {
            //Empty text in this box
        }
    }
}

Thanks.

Chuchuah answered 2/8, 2013 at 4:48 Comment(1)
It is considered polite to mark the correct answer.Psychic
P
3

This is how you do what you want.

public MainPage()
{
    this.InitializeComponent();
    Loaded += MainPage_Loaded;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    foreach (var textBox in AllTextBoxes(this))
    {
        textBox.Text = "Hello world";
    }
}

List<TextBox> AllTextBoxes(DependencyObject parent)
{
    var list = new List<TextBox>();
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        if (child is TextBox)
            list.Add(child as TextBox);
        list.AddRange(AllTextBoxes(child));
    }
    return list;
}

Reference: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

Best of luck!

Psychic answered 2/8, 2013 at 19:7 Comment(0)
C
1

You can do like this. Each page will have a container of UIElements, so I am using Grid. You can also do same with StackPanel also. I am iterating through its children and checking if it's Textbox or not.

XAML

<Grid x:Name="rootGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <TextBox Height="51" Margin="210,103,0,0" Text="TextBox" Width="135"/>
    <TextBox Height="51" Margin="459,149,0,0" Text="TextBox" Width="135"/>
    <TextBox Height="51" Margin="277,279,0,0" Text="TextBox" Width="135"/>
    <TextBox Height="51" Margin="580,279,0,0" Text="TextBox" Width="135"/>
    <TextBlock Height="63" Margin="227,494,0,0" Text="TextBlock" Width="142"/>
    <TextBlock Height="63" Margin="479,469,0,0" Text="TextBlock" Width="142"/>
    <TextBlock Height="63" Margin="573,406,0,0" Text="TextBlock" Width="142"/>
    <TextBlock Height="63" Margin="143,352,0,0" Text="TextBlock" Width="142"/>
    <CheckBox Content="CheckBox" Height="81" Margin="1064,203,0,0" Width="130"/>
    <CheckBox Content="CheckBox" Height="81" Margin="713,119,0,0" Width="130"/>
    <CheckBox Content="CheckBox" Height="81" Margin="831,352,0,0" Width="130"/>
</Grid>

C#

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    foreach (var child in rootGrid.Children)
    {
        if (child is TextBox)
        {
            System.Diagnostics.Debug.WriteLine(((TextBox)child).Text);
            if (string.IsNullOrEmpty(((TextBox)child).Text))
            {
                //Empty text in this box
            }
        }
    }
}
Candancecandela answered 2/8, 2013 at 5:3 Comment(4)
Thanks. But if I have Containers like : Grid Controls contain 3 Stack controls and each stack control contains a few textboxes. How to iterate this kind of structure? ThanksChuchuah
Give me your container structure, I will post solution for that.Candancecandela
No, this will not do it. This will only get the children of a single container, you need to make it recursive. I will answer with the solution so you can see what I mean.Psychic
Thanks. I have question: What is list.AddRange(AllTextBoxes(child)) And How the foreach statement to handle if textbox contains a number or Empty? int32.Parse(txtBox.text) in the foreach? This will handle if the Grid Container contain 3 or more stack container , each contain a few textboxes? ThanksChuchuah
R
1

//in asp.net c# if you have not masterpage

    foreach (Control ctrl in Page.Controls)
    {
        if (ctrl is TextBox)
        {

            ((TextBox)ctrl).Text = string.Empty;
        }
    }

/if you have master page then/

foreach (Control item in Page.Form.FindControl("ContentPlaceHolder1").Controls) { if (item is TextBox) { ((TextBox)item).Text = string.Empty; } }

Relly answered 4/1, 2017 at 10:47 Comment(1)
if you have masterpage than use this one: if you have master page then foreach (Control item in Page.Form.FindControl("ContentPlaceHolder1").Controls) { if (item is TextBox) { ((TextBox)item).Text = string.Empty; } }Relly

© 2022 - 2024 — McMap. All rights reserved.