Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:18:16+00:00 2026-05-24T07:18:16+00:00

foreach statement cannot operate on variables of type ‘System.Windows.Controls.GroupBox’ because ‘System.Windows.Controls.GroupBox’ does not contain

  • 0

foreach statement cannot operate on variables of type
‘System.Windows.Controls.GroupBox’ because
‘System.Windows.Controls.GroupBox’ does not contain a public
definition for ‘GetEnumerator’

mycode :

    foreach (var txt in this.groupBox1.Children)
        {
            if (txt is TextBox)
            {
                (txt as TextBox).Text = string.Empty;
            }
        }

But why is the correct code for the Grid ?

    foreach (var txt in this.MyGrid.Children)
    {
        if (txt is TextBox)
        {
            (txt as TextBox).Text = string.Empty;
        }
    }

What is the correct code for groupBox?

/////////////////editing

Correct code:

    foreach (var txt in this.MyGridInGroupBox.Children)
    {
        if (txt is TextBox)
        {
            (txt as TextBox).Text = string.Empty;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:18:16+00:00Added an answer on May 24, 2026 at 7:18 am

    Your first snippet won’t even compile (assuming groupBox1 is indeed a GroupBox), since GroupBox has no Children property.

    A GroupBox can only contain one child, represented by its Content property.

    If you need to iterate over all the visual children of a GroupBox, you might be able to use the VisualTreeHelper class. Something like this:

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(groupBox1); i++)
    {
        var txt = VisualTreeHelper.GetChild(groupBox1, i);
        if (txt is TextBox) ...
    }
    

    Update

    Ok, you’re saying that this doesn’t work, and I think I understand why.

    The VisualTreeHelper will only find the first-level visual children of the GroupBox, which (as the control is implemented) is a Grid.

    This is no good to you, because you need to recurse down into the children of the control and find all the TextBoxes.

    In that case, you’re better off using one of the many recursve “FindChildren” implementations around the web. Here’s one of mine:

    public static class DependencyObjectExtensions
    {
        public static IEnumerable<T> GetVisualChildren<T>(this DependencyObject depObj) 
            where T : DependencyObject
        {
            if (depObj == null) yield break;
    
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                var child = VisualTreeHelper.GetChild(depObj, i);
    
                var t = child as T;
                if (t != null) yield return t;
    
                foreach (var item in GetVisualChildren<T>(child))
                {
                    yield return item;
                }
            }
        }
    }
    

    You can use that like this:

    foreach (var txt in groupBox1.GetVisualChildren<TextBox>())
    {
        txt.Text = String.Empty;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am a student I am getting this error: forreach statement cannot operate
I cannot seem to get foreach to work. Maybe I do not understand it
I would like to convert the below foreach statement to a LINQ query that
How can i limit a foreach() statement? Say i only want it to run
In java a class can implement Iterable which lets you use the foreach() statement
I have a problem with a continue statement in my C# Foreach loop. I
I'm currently trying to return a match to a Foreach statement which is contained
I'm not sure if the issue is with the prepare() statement itself or with
I have not until now tried to use a foreach clause in a generic
Hi I've got problem with foreach statement. I have my controller method: public ActionResult

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.