首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么按字母顺序(ABCDEF)动态添加控件

2013-08-01 
怎样按字母顺序(ABCDEF)动态添加控件考试系统中题库设计时,我想动态添加选项,顺序按ABCDEF这样,点击一下按

怎样按字母顺序(ABCDEF)动态添加控件
考试系统中题库设计时,我想动态添加选项,顺序按ABCDEF这样,点击一下按钮添加A(radiobutton),再点击添加B,如此依次添加。本人比较菜,求达人写一个方法
[解决办法]
private int idx = 0;

添加按钮_Click(sender, e)
{
    RadioButton rb = new RadioButton();
    rb.Name = "rb" + ('A' + idx).ToString();
    rb.Text = ('A' + idx).ToString();
    rb.Top = 100 + rb.Height * idx;
    this.Controls.Add(rb);
}
[解决办法]

 private void button1_Click(object sender, EventArgs e)
        {
            string[] radioButtonList = new string[] { "A", "B", "C", "D", "E", "F", "G" };

            for (int i = 0; i < radioButtonList.Length; i++)
            {
                if (this.Controls[radioButtonList[i]] == null)
                {
                    RadioButton radioButton = new RadioButton();
                    radioButton.Name = radioButtonList[i];
                    radioButton.Text = radioButtonList[i];
                    radioButton.Width = 50;
                    radioButton.Location = new Point(100, 100 + i * 20);


                    this.Controls.Add(radioButton);
                    break;
                }
            }

        }

热点排行