一個項目中用到的實例,根據數據庫查詢出待顯示的radioButton的個數,顯示在一個新的窗口中。
//動態顯示radioButton 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.panel1.Controls.Add(radioButton[n]); top = top + 21; } }