Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

When you loop through each control you should be determining the type of control and not just assuming they are all GroupBox objects

The this.Controls collection contains all of your form first level controls, so not every control inside is a GroupBox. You need to get only GroupBox controls

Change your loop using this syntax

foreach (GroupBox box in this.Controls.OfType<GroupBox>())
{
   ....
}

Not every control in the Controls collection is a GroupBox (obviously), but the foreach is attempting to cast them inline.

So, change your loop to something like this:

foreach (var control in this.Controls)
{
    if(control is GroupBox)
    {
        if (control != MoveThis)
        {
            control.Left = (-1) * control.Width;
        }
        else
        {
            do
            {
                if (control.Left > 12)
                    control.Left--;
                else
                    control.Left++;
            }
            while (control.Left != 12);
        }
    }
}

EDIT: It's worth noting that this could potentially be very slow, since you're evaluating the type of every control in the form. The code that both John Kraft and Steve suggest should filter the controls collection to just those that are GroupBox, so that would likely yield better performance...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top