2013-08-02 10 views
5

私は多くのテキストボックスを追加しました。どのようにすべてのテキストボックスを繰り返したりループしたり、いくつかのチェックをします。各テキストボックスの内容が数字かどうかを確認します。現在のページのすべてのテキストボックスを反復する方法

以下はWinFormのコードですが、WinRTでの操作は?

foreach (Control item in GroupBox1.Controls) 
{ 

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

ありがとうございます。

+0

正解とマークするのは丁寧な考えです。おかげさまで –

答えて

1

このようにすることができます。各ページには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 
      } 
     } 
    } 
} 
+0

しかし、コンテナのような場合:グリッドコントロールには3つのスタックコントロールがあり、各スタックコントロールにはいくつかのテキストボックスが含まれています。このような構造を反復するには?ありがとう – MilkBottle

+0

あなたのコンテナ構造を教えてください、私はそのためのソリューションを投稿します。 – Xyroid

+0

いいえ、これはしません。これは、単一のコンテナの子を取得するだけで、再帰的にする必要があります。私は解決策に答えて、あなたは私が何を意味するかを見ることができます。 –

3

これは、あなたがやりたい方法です。

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

運のベスト! asp.net C#で

1

//マスターページを持っている場合は、foreachの(Page.Form.FindControlの制御項目(「ContentPlaceHolder1その後、/

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

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

/をマスターページしていない場合").Controls) { if(項目がTextBox) { ((TextBox)項目).Text = string.Empty; } }

+0

あなたは、このいずれかを使用よりも、マスターページを持っている場合(TextBox)項目).Text = string.Empty; } } –

関連する問題