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

  • Home
  • SEARCH
  • 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 8922715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:54:57+00:00 2026-06-15T06:54:57+00:00

The following code snip is throwing an InvalidCastException on the foreach loop : Unable

  • 0

The following code snip is throwing an InvalidCastException on the foreach loop :

Unable to cast object of type ‘System.Windows.Forms.StatusStrip’ to
type ‘System.Windows.Forms.GroupBox’.

I just dont understand how this is possible….
Then again I’m a noob so its probably something stupid.

     private void doSlide(GroupBox MoveThis)
    {
        //location 12,27
        var t = Task.Factory.StartNew(() =>
            {
                ExecuteSecure(() =>
                    {
                            foreach (GroupBox box in this.Controls)
                            {
                                if (box != MoveThis)
                                {
                                    box.Left = (-1) * box.Width;
                                }
                                else
                                {
                                    do
                                    {
                                        if (box.Left > 12)
                                            box.Left--;
                                        else
                                            box.Left++;
                                    }
                                    while (box.Left != 12);
                                }
                            }

                    });
            });
    }

Here is the code for Execute Secure

private void ExecuteSecure(Action a)
        {
            if (InvokeRequired)
                BeginInvoke(a);
            else a();
        }

Basically I have a form with a fixed size and several group boxes on the form, only 1 of which is visible at any given point. When we need to make a new GroupBox visible we call DoSlide(GroupBox) and specify the groupbox we want to make visible. It is then supposed to move every GroupBox on the form to the location (-Box.Width,27) except for the specified form which gets slide (incremented or decremented box.left) into view.

  • 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-06-15T06:54:58+00:00Added an answer on June 15, 2026 at 6:54 am

    You want to use

    this.Controls.OfType<GroupBox>()
    

    in your foreach. This.Controls returns all controls, not just GroupBoxes. The OfType<T> extension method filters the collection to the type you specify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: $.ajax({ type: GET, url: /Search/GetNewData //Controller/ActionMethod ---snip--- )}; This
I have a problem with the following snip of code: $.ajax( { type: POST,
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes
Given the following code (that doesn't work): while True: # Snip: print out current
I have the following code in a class that manages refreshing some object automatically
I am trying to make following snip of code lockless using interlocked operations, Any
I have the following code snip and am trying to debug it. Is there
Consider the following snip of java code byte b=(byte) 0xf1; byte c=(byte)(b>>4); byte d=(byte)

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.