【问题标题】:How to iterate all textboxes on current page如何迭代当前页面上的所有文本框
【发布时间】:2013-08-02 04:48:03
【问题描述】:

假设我添加了许多文本框。如何迭代或循环遍历所有文本框并进行一些检查。检查每个文本框的内容是否为数字。

下面是winForm的代码,在WinRT中怎么做?

foreach (Control item in GroupBox1.Controls)
{

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

谢谢。

【问题讨论】:

  • 标记正确答案被认为是礼貌的。

标签: c# windows-8 windows-store-apps winrt-xaml


【解决方案1】:

这就是你做你想做的事的方式。

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

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

祝你好运!

【讨论】:

    【解决方案2】:

    你可以这样做。每个页面都会有一个UIElements的容器,所以我使用的是Grid。你也可以对StackPanel 做同样的事情。我正在遍历它的孩子并检查它是否是Textbox

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

    【讨论】:

    • 谢谢。但是如果我有这样的容器: 网格控件包含 3 个堆栈控件,每个堆栈控件都包含一些文本框。如何迭代这种结构?谢谢
    • 给我你的容器结构,我会发布解决方案。
    • 不,这样不行。这只会得到单个容器的孩子,你需要让它递归。我会用解决方案来回答,这样你就可以明白我的意思了。
    • 谢谢。我有一个问题:什么是 list.AddRange(AllTextBoxes(child)) 以及如果文本框包含数字或空,foreach 语句如何处理? int32.Parse(txtBox.text) 在 foreach 中?如果 Grid Container 包含 3 个或更多 stack container ,每个包含几个文本框,这将处理?谢谢
    【解决方案3】:

    //如果没有masterpage,在asp.net c#中

        foreach (Control ctrl in Page.Controls)
        {
            if (ctrl is TextBox)
            {
    
                ((TextBox)ctrl).Text = string.Empty;
            }
        }
    

    /如果你有母版页,那么/

    foreach(Page.Form.FindControl("ContentPlaceHolder1").Controls 中的控件项) { 如果(项目是文本框) { ((TextBox)item).Text = string.Empty; } }

    【讨论】:

    • 如果你有masterpage而不是使用这个:如果你有masterpage然后foreach(Page.Form.FindControl(“ContentPlaceHolder1”).Controls中的控件项){如果(项目是TextBox){( (TextBox)item).Text = string.Empty; } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多