日期:2014-05-17  浏览次数:20667 次

怎样按字母顺序(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);