c#中的GetEnumerator问题

Vin*_*pin 0 c# winforms

我遇到了这个GetEnumerator问题..这是我的情况

        Panel eachPanel = new Panel();
        eachPanel.Size = new Size(pnlProcessCon.Width - 27, 24);
        eachPanel.Location = new Point(5, startPoint);
        eachPanel.BackColor = (defaultColor == alterColor[0]) ? alterColor[1] : alterColor[0];            

        TextBox txtProcess = new TextBox();
        txtProcess.Size = new Size(50, 20);
        txtProcess.Location = new Point(2,2);
        txtProcess.TextAlign = HorizontalAlignment.Center;
        txtProcess.Text = "P" + Convert.ToString(startProcess);

        TextBox txtBurstTime = new TextBox();
        txtBurstTime.Size = new Size(50, 20);
        txtBurstTime.Location = new Point(txtProcess.Right + 70, 2);
        txtBurstTime.TextAlign = HorizontalAlignment.Center;

        TextBox txtPriority = new TextBox();
        txtPriority.Size = new Size(50, 20);
        txtPriority.Location = new Point(txtBurstTime.Right + 70, 2);
        txtPriority.TextAlign = HorizontalAlignment.Center;

        eachPanel.Controls.Add(txtProcess);
        eachPanel.Controls.Add(txtBurstTime);
        eachPanel.Controls.Add(txtPriority);

        pnlProcessCon.Controls.Add(eachPanel);
Run Code Online (Sandbox Code Playgroud)

但当我调用他们的每个文本并添加到字典时,我收到此错误..

Error   1   foreach statement cannot operate on variables of type 'System.Windows.Forms.Panel' because 'System.Windows.Forms.Panel' does not contain a public definition for 'GetEnumerator'    C:\Users\vrynxzent@yahoo.com\Documents\Visual Studio 2008\Projects\Scheduler\Scheduler\Form1.cs 68  13  Scheduler
Run Code Online (Sandbox Code Playgroud)

并在这里得到我的错误..

foreach (var each in pnlProcessCon)
        {
            String[] temp = new String[3];
            foreach (var process in each)
            {
                temp = process.Text;                    
            }

        }
Run Code Online (Sandbox Code Playgroud)

Ada*_*ear 5

那里有一些问题.

首先,您应该枚举Controls集合.其次,TextBox在检索文本之前,必须先将每个控件转换为.第三,您声明temp为数组,因此无法直接为其指定字符串.第四届(如亨克Holterman指出的),你应该用实际的类型,而不是varforeach循环.

我将在这里尝试使用代码.您可以根据自己的需要进行调整.

TextBox txtProcess = new TextBox();
txtProcess.Name = "Process";
//configure other textboxes, add to panels, etc.

foreach (Panel each in pnlProcessCon.Controls)
{
    String[] temp = new String[3];
    foreach (Control control in each.Controls)
    {
        if(control.Name == "Process")
        {
            temp[0] = ((TextBox)control).Text;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @vrynxzent,你为什么要编程喝醉?! (3认同)