代码如下
//添加一个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;
                     }
                 }
             }           }

解决方案 »

  1.   

    你可以用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)) 这么写应该没有问题。
      

  2.   

    直接tCorl is RadioButton呢?
    或者
    foreach (Control tCorl in this.panel1.Controls)   
    {
       RadioButton trb = tCorl as RadioButton;
       if( trb != null )
       {
          ...
        }
     }
    代码我没试过,随便说说:)
      

  3.   

    用 Control.GetType().Name 去判断