关于容器中的控件遍历问题

alatook_seven 2007-04-02 08:26:37
代码如下
//添加一个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;
}
}
}

}
...全文
296 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganju 2008-07-31
  • 打赏
  • 举报
回复
不错
yuxing117 2007-04-03
  • 打赏
  • 举报
回复
用 Control.GetType().Name 去判断
xiaoaowanghu 2007-04-02
  • 打赏
  • 举报
回复
直接tCorl is RadioButton呢?
或者
foreach (Control tCorl in this.panel1.Controls)
{
RadioButton trb = tCorl as RadioButton;
if( trb != null )
{
...
}
}
代码我没试过,随便说说:)
steven1981 2007-04-02
  • 打赏
  • 举报
回复
你可以用tableLayoutPanel代替panel,自己计算layout好麻烦。
foreach (Control con in this.Controls)
{
if (con.GetType() == typeof(SagaViewerPanel.ClassContainer))
if (classID == ((SagaViewerPanel.ClassContainer)con).ClassID)
return true;
}
这是我做的一个project里的代码,if (con.GetType() == typeof(SagaViewerPanel.ClassContainer)) 这么写应该没有问题。
xray2005 2007-04-02
  • 打赏
  • 举报
回复
学习ing 。。。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧