0

I have a WPF with contain tab control (page1, page2, and page3).

In tab control page 2, I do have 3 groupbox (groupbox_A, groupbox_B, and groupbox_C), and each of the groupbox contain 3 textbox.

May I know what is the C# code to loop through all the textbox and clear the content.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user6648485
  • 77
  • 10

2 Answers2

0

This function will return all the textboxes within the tab control.

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

You can get all text boxes by enumerating like this :

foreach (var textbox in FindVisualChildren<TextBox>(window))
{
    // do something with tb here
}

Source : Find all controls in WPF Window by type

Community
  • 1
  • 1
ViVi
  • 4,339
  • 8
  • 29
  • 52
  • Thanks for sharing the code. It seem like I need to ensure the tab page 2 was activated so that the textbox store within page 2 will been clear. – user6648485 Jul 29 '16 at 00:19
  • I do have stackpanel_1 with orientation set to vertical. In the stackpanel_1, it consist of stackpanel_A, B, C, & D, with orientation set to horizontal and contain a few button. Some how code like foreach (stackepanel stkpnl in stackpanel_1.Children) was work. – user6648485 Jul 29 '16 at 00:27
  • The same thing happens here too. See `for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {` – ViVi Jul 29 '16 at 01:26
  • Glad to have helped you. :-) – ViVi Jul 29 '16 at 01:26
0

This doesn`t look like a good approach.. find another logic to clear your texts.. but if you just want to know how it would work...

the easiest way:

IEnumerable<myType> collection = control.Children.OfType<myType>();

(control is the root element of the window and myType would be groubBox in your case)

The microsoft way to get through all elements Link:

 // Enumerate all the descendants of the visual object.
 static public void EnumVisual(Visual myVisual)
 {
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
     {
        // Retrieve child visual at specified index value.
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

        // Do processing of the child visual object.

        // Enumerate children of the child visual object.
        EnumVisual(childVisual);
    }
 }
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
  • could you elaborate a bit more on the easiest way and Microsoft way as I still try to pick up the C# skill and apply to my job. (I use to practice in VB and just 2 week ago, I started with C#.) – user6648485 Jul 29 '16 at 00:29