内部foreach循环错误 [英] inner foreach loop error

查看:102
本文介绍了内部foreach循环错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

尝试:
foreach(Control k in ct.Controls)
   {
   ...
   }


OriginalGriff和Abhinav S的答案指出了您遇到的特定错误的原因.
但是,我想引起您的注意,在我看来代码的一个相当奇怪的方面是:

1.假设这是一个DragDrop处理程序.

2.您的外部迭代器:
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)

在(窗体,用户控件)的任何上下文类型的范围内对每个(顶级)控件进行迭代.
3.看来您然后将拖动的控件添加到每个Panel或每个GroupBox的每个实例.

一个.将一个控件的相同实例多次添加到相同或不同的容器控件中,将导致该控件位于"您添加到该控件的最后一个容器中的位置.

b.但是,如果您的窗体或UserControl在这里只有一个面板和一个GroupBox,则可以按预期工作.或者,很明显,如果Form或UserControl仅包含一个Panel或一个GroupBox,则这可能有效.

示例:如果我有一个Button,"button1",一个Panel,"panel1和一个GroupBox" groupBox1,并执行以下代码:

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:

panel1.Controls.Add(button1);
groupBox1.Controls.Add(button1);

在编译时不会引发任何错误,并且'button1最终位于'groupBox1内.

No error is thrown at compile time, and ''button1 ends up inside ''groupBox1.


您不能循环访问任何对象没有实现''GetEnumerator''.
ct是一个单独的控件,因此不能在其上运行foreach .
我不认为这是您真正想要做的.
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.


这篇关于内部foreach循环错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆