曲沐阳吧 关注:3贴子:239
  • 3回复贴,共1

C#中向Form里动态添控件

只看楼主收藏回复

RT~


IP属地:北京1楼2011-05-30 16:16回复
    C#中动态添加RadioButton,并将他们添加到一个GroupBox中。
    代码全手动添加,不借助Form Design.


    IP属地:北京2楼2011-05-30 16:16
    回复
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Windows.Forms;
      namespace RadioButtonTest
      {
           static class Program
           {
               /// <summary>
               /// The main entry point for the application.
               /// </summary>
               [STAThread]
               static void Main()
               {
                   Application.EnableVisualStyles();
                   Application.SetCompatibleTextRenderingDefault(false);
                   //Application.Run(new Form1());
                   int count = 2;            // Number of Radio button
                   Form form = new Form();
                   GroupBox groupBox1 = new System.Windows.Forms.GroupBox();
                   groupBox1.Location = new System.Drawing.Point(10, 10);
                   groupBox1.Size = new System.Drawing.Size(230, 56 + 35 * (count - 1));
                   groupBox1.Text = "";
                   for (int i = 0; i < count; i++)
                   {
                       RadioButton rb = new RadioButton();
                       rb.Text = "Test_" + i.ToString() + "_RadioButton";
                       rb.AutoSize = true;
                       rb.Location = new System.Drawing.Point(40, 25 + i * 35);
                       groupBox1.Controls.Add(rb);
                   }
                  
                   Button cmdCancel = new Button();
                   cmdCancel.Text = "Cancel";
                   cmdCancel.DialogResult = DialogResult.Cancel;
      


      IP属地:北京3楼2011-05-30 16:17
      回复
                     cmdCancel.Location = new System.Drawing.Point(165, groupBox1.Height + 20);
                     Button cmdOK = new Button();
                     cmdOK.Text = "OK";
                     cmdOK.DialogResult = DialogResult.OK;
                     cmdOK.Location = new System.Drawing.Point(80, groupBox1.Height + 20);
                     form.Text = "DPK file choose";
                     form.MaximizeBox = false;
                     form.MinimizeBox = false;
                     form.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                     form.SizeGripStyle = SizeGripStyle.Hide;
                     form.ClientSize = new System.Drawing.Size(250, groupBox1.Height + 54);
                     form.Controls.Add(groupBox1);
                     form.Controls.Add(cmdCancel);
                     form.Controls.Add(cmdOK);
                     if (form.ShowDialog() == DialogResult.OK)
                     {
                         foreach (RadioButton rb in groupBox1.Controls)
                         {
                             if (rb.Checked)
                             {
                                 MessageBox.Show(rb.Text);
                                 break;
                             }
                         }
                         MessageBox.Show("No select");
                     }
                     else
                     {
                         MessageBox.Show("Cancel");
                     }
                 }
             }
        }


        IP属地:北京4楼2011-05-30 16:17
        回复