关于容器中的控件遍历问题
代码如下
//添加一个RadioButton 控件 .然后计算新控件显示位置
public void addNpc(Image img, int atype, int asubtype)
{
RadioButton rb = new RadioButton();
rb.Appearance = Appearance.Button;
rb.AutoSize = true;
rb.Location = new System.Drawing.Point(0, 0);
rb.Size = new System.Drawing.Size(32, 32);
rb.AutoSize = false;
rb.BackgroundImage = img;
rb.BackgroundImageLayout = ImageLayout.Zoom;
rb.TabStop = true;
rb.UseVisualStyleBackColor = true;
rb.Tag = new Point(atype, asubtype);
this.panel1.Controls.Add(rb);
calculateSize();
}
private int rbNumInCol;
private int rbNumInRow;
public void calculateSize()
{
panel1.Width = this.Width;
rbNumInCol = panel1.Width / 50;
rbNumInRow = rbsCnt / rbNumInCol;
if (rbsCnt % rbNumInCol != 0) rbNumInRow++;
panel1.Height = rbNumInRow * 40;
int tx = 0;
int ty = 0;
foreach (Control tCorl in this.panel1.Controls)
{
//问题出现在这里.
/*
我需要判断panel1上面的所有RadioButton控件.然后做位置调整.
但是tCorl.GetType() is RadioButton得到的结果永远是false.
请教.这里应该如何写(我试过 tCorl.GetType() == typeof(RadioButton) 也不对.)
在watch里面出现的问题如下:
This expression causes side effects and will not be evaluated;
我应该如何处理这种情况
*/
if (tCorl.GetType() is RadioButton)
{
RadioButton trb = (RadioButton)tCorl;
trb.Left = tx;
trb.Top = ty;
tx += 50;
if (tx + 50 > panel1.Width)
{
ty += 40;
tx = 0;
}
}
}
}