Loading

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

考试系统中题库设计时,我想动态添加选项,顺序按ABCDEF这样,点击一下按钮添加A(radiobutton),再点击添加B,如此依次添加。本人比较菜,求达人写一个方法。

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;
          }
      }
}

换一下方法,横向加radiobutton。

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 + i * 50, 100);
               this.Controls.Add(radioButton);
               break;
           }
       }
}

 

posted @ 2016-07-05 10:48  guwei4037  阅读(636)  评论(1编辑  收藏  举报