Csharp/C Sharp/GUI Windows Form/GroupBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

CheckBox inside GroupBox

<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace Group {

 /// <summary>
 /// Summary description for FormGroup.
 /// </summary>
 public class FormGroup : System.Windows.Forms.Form
 {
   private System.Windows.Forms.GroupBox groupBox1;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.CheckBox checkBox1;
   private System.Windows.Forms.CheckBox checkBox2;
   private System.Windows.Forms.CheckBox checkBox3;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ruponentModel.Container components = null;
   public FormGroup()
   {
     //
     // Required for Windows Form Designer support
     //
     InitializeComponent();
     //
     // TODO: Add any constructor code after InitializeComponent call
     //
     Application.Idle += new System.EventHandler (OnIdle);
   }
   private void OnIdle (object sender, EventArgs e)
   {
     groupBox1.Enabled = checkBox3.Checked;
   }
   #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.checkBox3 = new System.Windows.Forms.CheckBox();
     this.checkBox2 = new System.Windows.Forms.CheckBox();
     this.textBox1 = new System.Windows.Forms.TextBox();
     this.groupBox1 = new System.Windows.Forms.GroupBox();
     this.checkBox1 = new System.Windows.Forms.CheckBox();
     this.groupBox1.SuspendLayout();
     this.SuspendLayout();
     // 
     // checkBox3
     // 
     this.checkBox3.Location = new System.Drawing.Point(24, 120);
     this.checkBox3.Name = "checkBox3";
     this.checkBox3.Size = new System.Drawing.Size(184, 16);
     this.checkBox3.TabIndex = 1;
     this.checkBox3.Text = "checkBox3";
     // 
     // checkBox2
     // 
     this.checkBox2.Location = new System.Drawing.Point(136, 56);
     this.checkBox2.Name = "checkBox2";
     this.checkBox2.Size = new System.Drawing.Size(104, 16);
     this.checkBox2.TabIndex = 2;
     this.checkBox2.Text = "checkBox2";
     // 
     // textBox1
     // 
     this.textBox1.Location = new System.Drawing.Point(16, 24);
     this.textBox1.Name = "textBox1";
     this.textBox1.Size = new System.Drawing.Size(232, 20);
     this.textBox1.TabIndex = 0;
     this.textBox1.Text = "textBox1";
     // 
     // groupBox1
     // 
     this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                         this.checkBox2,
                                         this.checkBox1,
                                         this.textBox1});
     this.groupBox1.Location = new System.Drawing.Point(16, 16);
     this.groupBox1.Name = "groupBox1";
     this.groupBox1.Size = new System.Drawing.Size(264, 96);
     this.groupBox1.TabIndex = 0;
     this.groupBox1.TabStop = false;
     this.groupBox1.Text = "groupBox1";
     // 
     // checkBox1
     // 
     this.checkBox1.Location = new System.Drawing.Point(16, 56);
     this.checkBox1.Name = "checkBox1";
     this.checkBox1.Size = new System.Drawing.Size(104, 16);
     this.checkBox1.TabIndex = 1;
     this.checkBox1.Text = "checkBox1";
     // 
     // FormGroup
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                     this.checkBox3,
                                     this.groupBox1});
     this.Name = "FormGroup";
     this.Text = "FormGroup";
     this.groupBox1.ResumeLayout(false);
     this.ResumeLayout(false);
   }
   #endregion
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main() 
   {
     Application.Run(new FormGroup());
   }
 }

}


      </source>


Group Box mouse enter event

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form {

   private System.Windows.Forms.GroupBox groupBox1;
   private System.Windows.Forms.CheckBox checkBox1;
   private System.Windows.Forms.CheckBox checkBox2;
   private System.Windows.Forms.CheckBox checkBox3;
   private System.Windows.Forms.RadioButton radioButton1;
   private System.Windows.Forms.RadioButton radioButton2;
   private System.Windows.Forms.RadioButton radioButton3;
   private System.Windows.Forms.Button button1;
   private System.ruponentModel.Container components = null;
   public Form1() {
     InitializeComponent();
   }
   private void InitializeComponent() {
           this.groupBox1 = new System.Windows.Forms.GroupBox();
           this.checkBox1 = new System.Windows.Forms.CheckBox();
           this.checkBox2 = new System.Windows.Forms.CheckBox();
           this.checkBox3 = new System.Windows.Forms.CheckBox();
           this.radioButton1 = new System.Windows.Forms.RadioButton();
           this.radioButton2 = new System.Windows.Forms.RadioButton();
           this.radioButton3 = new System.Windows.Forms.RadioButton();
           this.button1 = new System.Windows.Forms.Button();
           this.groupBox1.SuspendLayout();
           this.SuspendLayout();
           // 
           // groupBox1
           // 
           this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.radioButton1,
                                                                                   this.radioButton2,
                                                                                   this.radioButton3});
           this.groupBox1.Location = new System.Drawing.Point(8, 120);
           this.groupBox1.Name = "groupBox1";
           this.groupBox1.Size = new System.Drawing.Size(120, 144);
           this.groupBox1.TabIndex = 0;
           this.groupBox1.TabStop = false;
           this.groupBox1.Text = "Color";
           this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
           // 
           // checkBox1
           // 
           this.checkBox1.Location = new System.Drawing.Point(8, 8);
           this.checkBox1.Name = "checkBox1";
           this.checkBox1.TabIndex = 1;
           this.checkBox1.Text = "Circle";
           // 
           // checkBox2
           // 
           this.checkBox2.Location = new System.Drawing.Point(8, 40);
           this.checkBox2.Name = "checkBox2";
           this.checkBox2.TabIndex = 2;
           this.checkBox2.Text = "Rectangle";
           // 
           // checkBox3
           // 
           this.checkBox3.Location = new System.Drawing.Point(8, 72);
           this.checkBox3.Name = "checkBox3";
           this.checkBox3.TabIndex = 3;
           this.checkBox3.Text = "String";
           // 
           // radioButton1
           // 
           this.radioButton1.Location = new System.Drawing.Point(8, 32);
           this.radioButton1.Name = "radioButton1";
           this.radioButton1.TabIndex = 4;
           this.radioButton1.Text = "Red";
           this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
           // 
           // radioButton2
           // 
           this.radioButton2.Location = new System.Drawing.Point(8, 64);
           this.radioButton2.Name = "radioButton2";
           this.radioButton2.TabIndex = 5;
           this.radioButton2.Text = "Green";
           // 
           // radioButton3
           // 
           this.radioButton3.Location = new System.Drawing.Point(8, 96);
           this.radioButton3.Name = "radioButton3";
           this.radioButton3.TabIndex = 6;
           this.radioButton3.Text = "Blue";
           // 
           // button1
           // 
           this.button1.Location = new System.Drawing.Point(8, 280);
           this.button1.Name = "button1";
           this.button1.Size = new System.Drawing.Size(112, 32);
           this.button1.TabIndex = 4;
           this.button1.Text = "Draw";
           this.button1.Click += new System.EventHandler(this.button1_Click);
           // 
           // Form1
           // 
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(408, 317);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.button1,
                                                                         this.checkBox3,
                                                                         this.checkBox2,
                                                                         this.checkBox1,
                                                                         this.groupBox1});
           this.Name = "Form1";
           this.Text = "CheckBox and RadioButton Sample";
           this.groupBox1.ResumeLayout(false);
           this.ResumeLayout(false);
       }
       [STAThread]
       static void Main() 
       {
           Application.Run(new Form1());
       }
       private void groupBox1_Enter(object sender, System.EventArgs e)
       {
          Console.WriteLine("group box enter event");
       }
       private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
       {
          Console.WriteLine("Radio Button checked changed event");
       }
       private void button1_Click(object sender, System.EventArgs e)
       {
           Graphics g = Graphics.FromHwnd(this.Handle);
           String str = "";
           Rectangle rc = new Rectangle(150, 50, 250, 250);
           
           if(radioButton1.Checked)
           {
               str = "red";
           }
           if(radioButton2.Checked)
           {
               str+="Green";
           }
           if(radioButton3.Checked)
           {
               str+="Blue";
           }
           if (checkBox1.Checked)
           {
               str+="Ellipse";
           }
           if (checkBox2.Checked)
           {
               str += "Rectangle";
           }
           if (checkBox3.Checked)
           {
               g.FillRectangle(new SolidBrush(Color.White), rc);
               g.DrawString(str, new Font("Verdana", 12), new SolidBrush(Color.Black), rc);
           }
           
       }
   }


      </source>


Using GroupBoxes and Panels to hold buttons

<source lang="csharp">

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class GroupBoxPanelExample : System.Windows.Forms.Form
  {
     private System.Windows.Forms.Button hiButton;
     private System.Windows.Forms.Button byeButton;
     private System.Windows.Forms.Button leftButton;
     private System.Windows.Forms.Button rightButton;
     private System.Windows.Forms.GroupBox mainGroupBox;
     private System.Windows.Forms.Label messageLabel;
     private System.Windows.Forms.Panel mainPanel;
     public GroupBoxPanelExample()
     {
        InitializeComponent();
     }
     private void InitializeComponent()
     {
        this.messageLabel = new System.Windows.Forms.Label();
        this.byeButton = new System.Windows.Forms.Button();
        this.mainGroupBox = new System.Windows.Forms.GroupBox();
        this.hiButton = new System.Windows.Forms.Button();
        this.mainPanel = new System.Windows.Forms.Panel();
        this.rightButton = new System.Windows.Forms.Button();
        this.leftButton = new System.Windows.Forms.Button();
        this.mainGroupBox.SuspendLayout();
        this.mainPanel.SuspendLayout();
        this.SuspendLayout();
        this.messageLabel.Location = new System.Drawing.Point( 24, 144 );
        this.messageLabel.Name = "messageLabel";
        this.messageLabel.Size = new System.Drawing.Size( 176, 32 );
        this.messageLabel.TabIndex = 2;
        this.byeButton.Location = new System.Drawing.Point( 96, 48 );
        this.byeButton.Name = "byeButton";
        this.byeButton.Size = new System.Drawing.Size( 72, 32 );
        this.byeButton.TabIndex = 0;
        this.byeButton.Text = "B";
        this.byeButton.Click += new System.EventHandler( this.byeButton_Click );
        this.mainGroupBox.Controls.AddRange( new System.Windows.Forms.Control[] {
           this.byeButton, this.hiButton } );
        this.mainGroupBox.Location = new System.Drawing.Point( 24, 24 );
        this.mainGroupBox.Name = "mainGroupBox";
        this.mainGroupBox.Size = new System.Drawing.Size( 176, 104 );
        this.mainGroupBox.TabIndex = 0;
        this.mainGroupBox.TabStop = false;
        this.mainGroupBox.Text = "Main GroupBox";
        this.hiButton.Location = new System.Drawing.Point( 8, 48 );
        this.hiButton.Name = "hiButton";
        this.hiButton.Size = new System.Drawing.Size( 72, 32 );
        this.hiButton.TabIndex = 0;
        this.hiButton.Text = "A";
        this.hiButton.Click += new System.EventHandler(this.hiButton_Click );
        this.mainPanel.AutoScroll = true;
        this.mainPanel.BorderStyle =System.Windows.Forms.BorderStyle.FixedSingle;
        this.mainPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
           this.rightButton,this.leftButton } );
        this.mainPanel.Location =new System.Drawing.Point( 24, 192 );
        this.mainPanel.Name = "mainPanel";
        this.mainPanel.Size =new System.Drawing.Size( 176, 112 );
        this.mainPanel.TabIndex = 1;
        this.rightButton.Location =new System.Drawing.Point( 264, 40 );
        this.rightButton.Name = "rightButton";
        this.rightButton.Size =new System.Drawing.Size( 80, 40 );
        this.rightButton.TabIndex = 0;
        this.rightButton.Text = "Far Right";
        this.rightButton.Click += new System.EventHandler(this.rightButton_Click );
        this.leftButton.Location =new System.Drawing.Point( 8, 40 );
        this.leftButton.Name = "leftButton";
        this.leftButton.Size =new System.Drawing.Size( 80, 40 );
        this.leftButton.TabIndex = 0;
        this.leftButton.Text = "Far Left";
        this.leftButton.Click +=new System.EventHandler( this.leftButton_Click );
        this.AutoScaleBaseSize =new System.Drawing.Size( 5, 13 );
        this.ClientSize =new System.Drawing.Size( 224, 317 );
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
              this.messageLabel,this.mainPanel,this.mainGroupBox } );
        this.Name = "GroupBoxPanelExample";
        this.Text = "GroupBoxPanelExample";
        this.mainGroupBox.ResumeLayout( false );
        this.mainPanel.ResumeLayout( false );
        this.ResumeLayout( false );
     }
     [STAThread]
     static void Main() 
     {
        Application.Run( new GroupBoxPanelExample() );
     }
     private void hiButton_Click( object sender, System.EventArgs e )
     {
        messageLabel.Text= "A pressed";
     }
     private void byeButton_Click(object sender, System.EventArgs e )
     {
        messageLabel.Text = "B pressed";
     }
     private void leftButton_Click(object sender, System.EventArgs e )
     {
        messageLabel.Text = "Far left pressed";
     }
     private void rightButton_Click( 
        object sender, System.EventArgs e )
     {
        messageLabel.Text = "Far right pressed";
     }
  }


      </source>