简体   繁体   English

c# 中的 GetEnumerator 问题

[英]GetEnumerator problem in c#

i got this GetEnumerator problem.. here's my situation我遇到了这个 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);

but when i call each of their text and add to dictionary, i got this error..但是当我调用他们的每个文本并添加到字典时,我得到了这个错误..

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

and got my error here..并在这里得到了我的错误..

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

        }

There are a few issues there.那里有几个问题。

First, you should enumerate the Controls collections.首先,您应该枚举控件 collections。 Second, you will have to cast each control to TextBox before you can retrieve the text.其次,您必须先将每个控件转换为TextBox ,然后才能检索文本。 Third, you declared temp as an array, so you can't directly assign a string to it.第三,您将temp声明为数组,因此您不能直接为其分配字符串。 Fourth (as Henk Holterman pointed out), you should use actual types and not var in the foreach loops.第四(正如 Henk Holterman 指出的那样),您应该在foreach循环中使用实际类型而不是var

I'm going to take a stab at working code here.我将在这里尝试工作代码。 Feel free to adjust for your own needs.随意调整自己的需要。

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;
        }
    }
}

This would be the general idea (note: no var )这将是一般的想法(注意:没有var

foreach (Panel p in pnlProcessCon.Controls)
{
    foreach (Control process in p.Controls)
    {
    }                    
}

But make sure pnlProcessCon only contains Panels.但请确保pnlProcessCon仅包含面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM