GetEnumerator problem in c#
i got this GetEnumerator problem.. here's my situation
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. Second, you will have to cast each control to TextBox
before you can retrieve the text. Third, you declared temp
as an array, so you can't directly assign a string to it. Fourth (as Henk Holterman pointed out), you should use actual types and not var
in the foreach
loops.
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
)
foreach (Panel p in pnlProcessCon.Controls)
{
foreach (Control process in p.Controls)
{
}
}
But make sure pnlProcessCon
only contains Panels.
精彩评论