Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
C#
void pan_DragDrop(object sender, DragEventArgs e)
        {
           
            try
            {
                Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
                if (c != null)
                {

                    c.Location = c.PointToClient(new Point(e.X, e.Y));
                    foreach (Control ct in this.Controls)
                    {
                        
                        if (ct is Panel)
                        {                         
                            ct.Controls.Add(c);
                            c.Location = new Point(0, c.Height+i1);
                            ct.AllowDrop = true;
                            i1+=7;
<HERE GETTING AN ERROR foreach(Control k in ct)
                            {
                            
                            
                            
                            }
                                  
                        }
                        else if(ct is GroupBox)
                        {
                            ct.Controls.Add(c);
                            c.Location = new Point(0, c.Height + i1);
                            ct.AllowDrop = true;
                            i1 += 7;
                        }
                        
                    }


                }



            }
            catch(Exception ex)
            {
              MessageBox.Show(ex.ToString());
            }
        }


///GETTING AN ERROR
Error	1	foreach statement cannot operate on variables of type 'System.Windows.Forms.Control' because 'System.Windows.Forms.Control' does not contain a public definition for 'GetEnumerator'	D:\Assignment\Assignment -Till Now\Form_For_container_usercontrol\Form_For_container_usercontrol\Form3.cs	263	29	Form_For_container_usercontrol
Posted
Updated 10-Dec-11 0:36am
v2
Comments
BillWoodruff 10-Dec-11 19:56pm    
I think it would improve your question here if you stated your "goal" in this code, because it appears to me that what you are doing is very different from what normally is done in a DragDrop Event Handler, if that's what this, which seems likely.

Please see the concerns raised in my answer below for a full explanation of why I find your "goal" here difficult to understand.

Try:
C#
foreach(Control k in ct.Controls)
   {
   ...
   }
 
Share this answer
 
Comments
OriginalGriff 10-Dec-11 7:06am    
Every Control has a Controls List - it doesn't have to be a container. For most of them, adding something to this list is not helpful, but it exists and you can add to it, or enumerate through it.
In this case, since he has tested for a Panel beforehand, it will work exactly as you would expect. Personally, I would cast the control to a Panel just to be obvious, but it isn't strictly necessary.
Abhinav S 10-Dec-11 8:23am    
Right. Makes sense.
BillWoodruff 10-Dec-11 19:51pm    
Please see the concern, expressed in my answer below, that he is, potentially, adding the dragged Panel more than once because he's iterating over every Control in the context here by using "this.Controls."

Certainly agree with you the code would be more 'readable' if he went ahead and cast to a Panel; however, both of us are making the assumption, based on the prefix "pan_" that this is a Panel ... and how dangerous assumptions on QA can be ... :)
OriginalGriff 11-Dec-11 3:20am    
Actually, I was basing it on the line:
if (ct is Panel)
I hadn't even read the method name! :laugh:
BillWoodruff 11-Dec-11 3:54am    
And, I assumed that what you advocated casting to a Panel was "c" which is the (maybe: we guess ?) Panel being drag-dropped here :)
OriginalGriff's and Abhinav S's answers point out to you the reason for the specific error you are getting.

However, I'd like to draw your attention to what appears to me a rather strange aspect of your code:

1. assuming this is a DragDrop handler.

2. your outer iterator:
foreach (Control ct in this.Controls)
Is iterating over every (top level) Control in the scope of whatever type of context it's in (Form, UserControl).

3. it appears that you then add the dragged Control to every instance of every Panel, or every GroupBox.

a. adding the same instance of a Control multiple times to the same, or to different, container Controls, is going to result in the place where the Control "is" being "in" the last container Control you added it to.

b. however, if it's the case that your Form, or UserControl, here has only one Panel, and one GroupBox on it: this could work as expected. Or, this could work, obviously, if the Form, or UserControl, contains only one Panel, or one GroupBox.

Example: if I have a Button, 'button1, a Panel, 'panel1, and a GroupBox, 'groupBox1, and execute this code:
C#
panel1.Controls.Add(button1);
groupBox1.Controls.Add(button1);
No error is thrown at compile time, and 'button1 ends up inside 'groupBox1.
 
Share this answer
 
v2
Comments
Abhinav S 10-Dec-11 23:25pm    
This is a good explanation. 5.
[no name] 12-Dec-11 20:40pm    
5 as well
You cannot loop over any object that does not implement 'GetEnumerator'.
ct is an individual control and hence you cannot run a foreach over it.
I don't think this is what you really want to do.
 
Share this answer
 
solved
solved
solved
solved
solved
solved
solved
solved
solved
solved
 
Share this answer
 
Comments
lukeer 12-Dec-11 8:00am    
Then go to the solution (or solutions) that helped you and accept it as an answer.
That way the poster of the helping solution gets credited and will gladly help again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900