Winform 动态添加RadioButton、CheckBox 且任意分组

</pre><pre name="code" class="csharp">//按列分组(n>i且listSize>n)
<span style="white-space:pre">	</span>public void showRadioButton(int listSize, List<String> list)
        {
            //控件上边缘与容器上边缘的距离  
            int top = 6;
            //记录循环,控件左上角相对于容器左上角的坐标  
            int i = 0;
            int n = 0;
            RadioButton[] radioButton = new RadioButton[listSize];
            for (n = 0; n < listSize; n++)
            {
                if (n % 15 == 0 && n != 0)
                {
                    i++;
                    if (i % 5 == 0)
                    {
                        top = 6 * n;
                        i = 0;
                    }
                    else
                    {
                        top = 6;
                    }
                }
                radioButton[n] = new RadioButton();
                radioButton[n].AutoSize = true;
                radioButton[n].Top = top;
                //控件左上角相对于容器左上角的坐标,以及每个控件之间的距离  
                radioButton[n].Location = new Point(i * 150 + 2, top);
                // MessageBox.Show(name[n].ToString());  
                radioButton[n].Text = list[n].ToString();
                radioButton[n].Visible = true;
                radioButton[n].Name = "radioButton" + n;
                this.panelMain.Controls.Add(radioButton[n]);
                top = top + 21;
            }

        }
//按行分组
        public void showRadioButton(int listSize)
        {
            //控件上边缘与容器上边缘的距离  
            int top = 6;
            //记录循环,控件左上角相对于容器左上角的坐标  
            int i = 0;
            int n = 0;
            GroupBox gb = new GroupBox();
            panelMain.AutoScroll = true;
            RadioButton[] radioButton = new RadioButton[listSize];
            for (n = 0; n < listSize; n++)
            {
                if (n % 4 == 0 && n != 0)
                {
                    top += 20;
                    i = 0;
                 
                }
                radioButton[n] = new RadioButton();
                radioButton[n].AutoSize = true;
                radioButton[n].Top = top;
                //控件左上角相对于容器左上角的坐标,以及每个控件之间的距离  
                radioButton[n].Location = new Point(i* 150 + 2, top);
                // MessageBox.Show(name[n].ToString());  
                radioButton[n].Text = n.ToString();
                radioButton[n].Visible = true;
                radioButton[n].Name = "radioButton" + n;
                this.panelMain.Controls.Add(radioButton[n]);
                i++;
            }

        }  
 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值