Csharp/C Sharp/GUI Windows Form/Form Frame Window — различия между версиями

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

Текущая версия на 11:33, 26 мая 2010

Add controls to a form

/*
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 AddControls
{
  /// <summary>
  /// Summary description for AddControls.
  /// </summary>
  public class AddControls : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public AddControls()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #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.button2 = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(160, 240);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(96, 24);
      this.button2.TabIndex = 3;
      this.button2.Text = "Cancel";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(38, 200);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(216, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(48, 240);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(80, 24);
      this.button1.TabIndex = 2;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(38, 32);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(216, 147);
      this.listBox1.TabIndex = 0;
      // 
      // Form1
      // 
      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.button2,
                                      this.button1,
                                      this.textBox1,
                                      this.listBox1});
      this.Name = "AddControls";
      this.Text = "AddControls";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new AddControls());
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
      if (textBox1.Text == "")
        return;
      string strAdd = textBox1.Text;
      if (listBox1.FindString (strAdd, -1) < 0)
      {
        listBox1.Items.Add (strAdd);
        textBox1.Text = "";
        textBox1.Focus ();
        return;
      }
      MessageBox.Show ("\"" + strAdd + "\" is already in the list box", "Duplicate");
    }
    private void button2_Click(object sender, System.EventArgs e)
    {
      Application.Exit();
    }
  }
}


Add control to a form window

using System;
using System.Windows.Forms;
using System.Drawing;
public class PushMe2 : Form {
  Button pushMeButton;
  public PushMe2() {
    pushMeButton = new Button(); 
    pushMeButton.Text = "Push Me";
    pushMeButton.Height = 60;
    pushMeButton.Width = 80;
    pushMeButton.Top = 60;
    pushMeButton.Left = 60;
    pushMeButton.Click += new EventHandler(ButtonClicked);
    this.Controls.Add(pushMeButton);
    this.Height = 200;
    this.Width = 200;
    this.StartPosition = FormStartPosition.CenterScreen;
    this.Visible = true;
  }
  public void ButtonClicked(object source, EventArgs e) {
    Button b = (Button)source;
    if ( b.Text == "Push Me" ) {
      b.Text = "Ouch";
    }
    else {
      b.Text = "Push Me";
    }
  }
  static void Main() {
    Application.Run(new PushMe2());
  }
}


A form-based Windows Skeleton

// A form-based Windows Skeleton. 
 
using System; 
using System.Windows.Forms; 
 
// WinSkel is derived from Form. 
public class WinSkel : Form { 
 
  public WinSkel() { 
    // Give the window a name. 
    Text = "A Windows Skeleton"; 
  }   
 
  // Main is used only to start the application. 
  [STAThread] 
  public static void Main() { 
    WinSkel skel = new WinSkel(); // create a form 
 
    // Start the window running. 
    Application.Run(skel); 
  } 
}


Assign Form window default value

using System;
using System.Drawing;
using System.Windows.Forms;
public class EnterPrice : Form {
  private Button enter = new Button();
  private Label answer = new Label();
  private TextBox text = new TextBox( );
  public EnterPrice( ) {
    enter.Text = "Enter Price";
    text.Text = "";
    answer.Text = "";
    Size = new Size(300,200);
    answer.Size = new Size(200,50);
    enter.Location = new Point(30 + enter.Width, 30);
    text.Location = new Point (40 + enter.Width + enter.Width, 30);
    answer.Location = new Point(20, 60);
    AcceptButton = enter;
    Controls.Add(text);
    Controls.Add(answer);
    Controls.Add(enter);
    enter.Click += new EventHandler(Enter_Click);
  }
  protected void Enter_Click(Object sender, EventArgs e) {
    try{
    Console.WriteLine(Double.Parse(text.Text));
    }catch(Exception){
    }
    text.Text = "";
    text.Focus();
  }
  static void Main() {
    Application.Run(new EnterPrice());
  }
}


Auto scroll form window

  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.Label label1;
    private System.ruponentModel.Container components;
  
    public Form1()
    {
      InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    private void InitializeComponent()
    {
      this.label1 = new System.Windows.Forms.Label();
      this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F);
      this.label1.Location = new System.Drawing.Point(16, 16);
      this.label1.Size = new System.Drawing.Size(376, 224);
      this.label1.TabIndex = 0;
      this.label1.Text = "Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ";
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.AutoScroll = true;
      this.AutoScrollMinSize = new System.Drawing.Size(300, 300);
      this.ClientSize = new System.Drawing.Size(403, 256);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label1});
      this.Text = "Form1";
    }
    [STAThread]
    static void Main() 
    {
      Application.Run(new Form1());
    }
  }


AutoScrollPosition

 

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class Form1 : Form {
    private Rectangle rectangleBounds = new Rectangle(new Point(0, 0), new Size(200, 200));
    private Rectangle ellipseBounds = new Rectangle(new Point(50, 200), new Size(200, 150));
    private Pen bluePen = new Pen(Color.Blue, 3);
    private Pen redPen = new Pen(Color.Red, 2);
    private Brush solidAzureBrush = Brushes.Azure;
    private Brush solidYellowBrush = new SolidBrush(Color.Yellow);
    static private Brush brickBrush = new HatchBrush(HatchStyle.DiagonalBrick,Color.DarkGoldenrod, Color.Cyan);
    private Pen brickWidePen = new Pen(brickBrush, 10);
    public Form1() {
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        Graphics dc = e.Graphics;
        Point scrollOffset = this.AutoScrollPosition;
        dc.TranslateTransform(scrollOffset.X, scrollOffset.Y);
        if (e.ClipRectangle.Top + scrollOffset.X < 350 ||
            e.ClipRectangle.Left + scrollOffset.Y < 250) {
            dc.DrawRectangle(bluePen, rectangleBounds);
            dc.FillRectangle(solidYellowBrush, rectangleBounds);
            dc.DrawEllipse(redPen, ellipseBounds);
            dc.FillEllipse(solidAzureBrush, ellipseBounds);
            dc.DrawLine(brickWidePen, rectangleBounds.Location,ellipseBounds.Location + ellipseBounds.Size);
        }
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Center form window

  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 Button myButton; 
    public Form1()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
        
            this.StartPosition=FormStartPosition.CenterScreen;
//      CenterToScreen();
      myButton = new Button();
      myButton.Text = "www.nfex.ru";
      myButton.Location = new System.Drawing.Point(64, 32);
      myButton.Size = new System.Drawing.Size(150, 50);
      Controls.Add(myButton);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
  }


Change Form window background

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
    private System.ruponentModel.IContainer components;
  public Form1(){
    InitializeComponent();
  }

  #region Windows Form Designer generated code
  private void InitializeComponent(){
      this.ruponents = new System.ruponentModel.Container();
      this.timer1 = new System.Windows.Forms.Timer(this.ruponents);
      this.timer1.Enabled = true;
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(292, 260);
      this.Name = "Form1";
      this.Text = "LinearGradientBrush Demo";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
  #endregion
  static void Main(){
    Application.Run(new Form1());
  }
    private void Form1_Load(object sender, System.EventArgs e) {
      this.BackColor = Color.FromArgb(255, 0, 0, 255);  
    }
    private System.Windows.Forms.Timer timer1;
    private float angle = 0;
    private LinearGradientBrush GetBrush()
    {
      return new LinearGradientBrush(
        new Rectangle( 20, 20, 200, 100),
        Color.Orange,
        Color.Yellow,
        0.0F,
        true);
    }

    private void Rotate( Graphics graphics, LinearGradientBrush brush )
    {
      brush.RotateTransform(angle);
      brush.SetBlendTriangularShape(.5F);
      graphics.FillRectangle(brush, brush.Rectangle);
    }
    private void Rotate(Graphics graphics)
    {
      angle += 5 % 360;
      Rotate(graphics, GetBrush());
    }
    private void timer1_Tick(object sender, System.EventArgs e)
    {
      Rotate(CreateGraphics());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Rotate(e.Graphics);
    }
}


Change Form window ownership

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class ChildForm : Form
{
  public System.Windows.Forms.Label lblState;
  public ChildForm()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.lblState = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblState
    // 
    this.lblState.Location = new System.Drawing.Point(26, 24);
    this.lblState.Name = "lblState";
    this.lblState.Size = new System.Drawing.Size(184, 56);
    this.lblState.TabIndex = 2;
    // 
    // ChildForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(236, 104);
    this.Controls.Add(this.lblState);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "ChildForm";
    this.Text = "ChildForm";
    this.ResumeLayout(false);
  }
  
}
public class ParentForm : Form{
  private System.Windows.Forms.Button cmdReleaseOwnership;
  private System.Windows.Forms.Button cmdAddOwnership;
  public ParentForm()
  {
    InitializeComponent();
    frmOwned.Show();
  }
  private ChildForm frmOwned = new ChildForm();

  private void cmdAddOwnership_Click(object sender, System.EventArgs e)
  {
    this.AddOwnedForm(frmOwned);
    frmOwned.lblState.Text = "Owned. Minimize me to see the difference.";
  }
  private void cmdReleaseOwnership_Click(object sender, System.EventArgs e)
  {
    this.RemoveOwnedForm(frmOwned);
    frmOwned.lblState.Text = "Not owned! Minimize me to see the difference.";
  }
  private void InitializeComponent()
  {
    this.cmdReleaseOwnership = new System.Windows.Forms.Button();
    this.cmdAddOwnership = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // cmdReleaseOwnership
    // 
    this.cmdReleaseOwnership.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.cmdReleaseOwnership.Location = new System.Drawing.Point(150, 197);
    this.cmdReleaseOwnership.Name = "cmdReleaseOwnership";
    this.cmdReleaseOwnership.Size = new System.Drawing.Size(128, 32);
    this.cmdReleaseOwnership.TabIndex = 6;
    this.cmdReleaseOwnership.Text = "Remove Ownership";
    this.cmdReleaseOwnership.Click += new System.EventHandler(this.cmdReleaseOwnership_Click);
    // 
    // cmdAddOwnership
    // 
    this.cmdAddOwnership.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.cmdAddOwnership.Location = new System.Drawing.Point(14, 197);
    this.cmdAddOwnership.Name = "cmdAddOwnership";
    this.cmdAddOwnership.Size = new System.Drawing.Size(120, 32);
    this.cmdAddOwnership.TabIndex = 5;
    this.cmdAddOwnership.Text = "Set Ownership";
    this.cmdAddOwnership.Click += new System.EventHandler(this.cmdAddOwnership_Click);
    // 
    // ParentForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.cmdReleaseOwnership);
    this.Controls.Add(this.cmdAddOwnership);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "ParentForm";
    this.Text = "Owner";
//    this.Load += new System.EventHandler(this.ParentForm_Load);
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new ParentForm());
  }
}


Change the background and text colors of a form using Color Dialog

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;

   public class ShowColorsComplex : System.Windows.Forms.Form
   {
      private System.Windows.Forms.Button backgroundColorButton;
      private System.Windows.Forms.Button textColorButton;
      public ShowColorsComplex()
      {
         InitializeComponent();
      }
      private void InitializeComponent()
      {
         this.backgroundColorButton = new System.Windows.Forms.Button();
         this.textColorButton = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // backgroundColorButton
         // 
         this.backgroundColorButton.Location = new System.Drawing.Point(16, 16);
         this.backgroundColorButton.Name = "backgroundColorButton";
         this.backgroundColorButton.Size = new System.Drawing.Size(264, 32);
         this.backgroundColorButton.TabIndex = 0;
         this.backgroundColorButton.Text = "Change Background Color";
         this.backgroundColorButton.Click += new System.EventHandler(this.backgroundColorButton_Click);
         // 
         // textColorButton
         // 
         this.textColorButton.Location = new System.Drawing.Point(16, 64);
         this.textColorButton.Name = "textColorButton";
         this.textColorButton.Size = new System.Drawing.Size(264, 32);
         this.textColorButton.TabIndex = 1;
         this.textColorButton.Text = "Change Text Color";
         this.textColorButton.Click += new System.EventHandler(this.textColorButton_Click);
         // 
         // ShowColorsComplex
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(292, 109);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.textColorButton,
                                                                      this.backgroundColorButton});
         this.Name = "ShowColorsComplex";
         this.Text = "ShowColorsComplex";
         this.ResumeLayout(false);
      }
      static void Main() 
      {
         Application.Run( new ShowColorsComplex() );
      }
      private void textColorButton_Click(object sender, System.EventArgs e ){
         ColorDialog colorChooser = new ColorDialog();
         DialogResult result;
         result = colorChooser.ShowDialog();
         if ( result == DialogResult.Cancel )
            return;
         
         backgroundColorButton.ForeColor = colorChooser.Color;
         textColorButton.ForeColor = colorChooser.Color;
      }
      private void backgroundColorButton_Click(object sender, System.EventArgs e ){
         ColorDialog colorChooser = new ColorDialog();
         DialogResult result;
         colorChooser.FullOpen = true;
         result = colorChooser.ShowDialog();
         if ( result == DialogResult.Cancel )
            return;
         this.BackColor = colorChooser.Color;
      }
   }


Create Graphics Object from form window handle

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


Demonstrates creating a form in a console program

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Form.cs -- Demonstrates creating a form in a console program
//
//            Compile this program with the following command line:
//                C:>csc Form.cs
using System;
using System.Windows.Forms;
namespace nsForm
{
    public class clsMainForm
    {
        static public void Main ()
        {
            Application.Run(new Form());
        }
    }
}


Draw image based on the window size

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("winter.jpg");
      Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
      g.DrawImage(bmp, this.ClientRectangle);
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Form for data input

/*
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 Employee
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class EmployeeForm1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public EmployeeForm1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #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.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(48, 240);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(80, 24);
      this.button1.TabIndex = 2;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(160, 240);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(96, 24);
      this.button2.TabIndex = 3;
      this.button2.Text = "Cancel";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(38, 32);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(216, 186);
      this.listBox1.TabIndex = 0;
      this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_OnDoubleClick);
      // 
      // Form1
      // 
      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.listBox1,
                                      this.button2,
                                      this.button1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new EmployeeForm1());
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
      EMPLOYEE emp = new EMPLOYEE();
      EmpForm eform = new EmpForm (emp);
      if (eform.ShowDialog (this) == DialogResult.Cancel)
        return;
      eform.GetEmployeeData (out emp);
      listBox1.Items.Add (emp);
    }
    private void button2_Click(object sender, System.EventArgs e)
    {
      Application.Exit ();
    }
    private void listBox1_OnDoubleClick(object sender, System.EventArgs e)
    {
      int index = listBox1.SelectedIndex;
      if (index < 0)
        return;
      EMPLOYEE emp = (EMPLOYEE) listBox1.Items[index];
      EmpForm eform = new EmpForm (emp);
      if (eform.ShowDialog (this) == DialogResult.Cancel)
        return;
      eform.GetEmployeeData (out emp);
      listBox1.Items.RemoveAt (index);
      listBox1.Items.Insert (index,emp);
    }
  }
  public struct EMPLOYEE
  {
    public string  FirstName;
    public string  LastName;
    public string  Address;
    public string  City;
    public string  State;
    public string  Zip;
    public override string ToString ()
    {
      return (LastName + ", " + FirstName);
    }
  }
  /// <summary>
  /// Summary description for EmpForm.
  /// </summary>
  public class EmpForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.TextBox textBox5;
    private System.Windows.Forms.TextBox textBox6;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public EmpForm(EMPLOYEE emp)
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      textBox1.Text = emp.FirstName;
      textBox2.Text = emp.LastName;
      textBox3.Text = emp.Address;
      textBox4.Text = emp.City;
      textBox5.Text = emp.State;
      textBox6.Text = emp.Zip;
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #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.textBox4 = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.label3 = new System.Windows.Forms.Label();
      this.textBox5 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.label5 = new System.Windows.Forms.Label();
      this.label6 = new System.Windows.Forms.Label();
      this.textBox2 = new System.Windows.Forms.TextBox();
      this.textBox3 = new System.Windows.Forms.TextBox();
      this.label4 = new System.Windows.Forms.Label();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.textBox6 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // textBox4
      // 
      this.textBox4.Location = new System.Drawing.Point(16, 182);
      this.textBox4.Name = "textBox4";
      this.textBox4.Size = new System.Drawing.Size(120, 20);
      this.textBox4.TabIndex = 3;
      this.textBox4.Text = "";
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(16, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(136, 16);
      this.label1.TabIndex = 8;
      this.label1.Text = "First Name";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(16, 64);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(128, 16);
      this.label2.TabIndex = 9;
      this.label2.Text = "Last Name";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(16, 109);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(120, 19);
      this.label3.TabIndex = 10;
      this.label3.Text = "Address";
      // 
      // textBox5
      // 
      this.textBox5.Location = new System.Drawing.Point(168, 184);
      this.textBox5.Name = "textBox5";
      this.textBox5.Size = new System.Drawing.Size(32, 20);
      this.textBox5.TabIndex = 4;
      this.textBox5.Text = "";
      // 
      // button1
      // 
      this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
      this.button1.Location = new System.Drawing.Point(48, 224);
      this.button1.Name = "button1";
      this.button1.TabIndex = 6;
      this.button1.Text = "OK";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.CausesValidation = false;
      this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this.button2.Location = new System.Drawing.Point(160, 224);
      this.button2.Name = "button2";
      this.button2.TabIndex = 7;
      this.button2.Text = "Cancel";
      // 
      // label5
      // 
      this.label5.Location = new System.Drawing.Point(168, 163);
      this.label5.Name = "label5";
      this.label5.Size = new System.Drawing.Size(40, 16);
      this.label5.TabIndex = 12;
      this.label5.Text = "State";
      // 
      // label6
      // 
      this.label6.Location = new System.Drawing.Point(224, 163);
      this.label6.Name = "label6";
      this.label6.Size = new System.Drawing.Size(48, 16);
      this.label6.TabIndex = 13;
      this.label6.Text = "Zip";
      // 
      // textBox2
      // 
      this.textBox2.Location = new System.Drawing.Point(16, 82);
      this.textBox2.Name = "textBox2";
      this.textBox2.Size = new System.Drawing.Size(208, 20);
      this.textBox2.TabIndex = 1;
      this.textBox2.Text = "";
      // 
      // textBox3
      // 
      this.textBox3.Location = new System.Drawing.Point(16, 132);
      this.textBox3.Name = "textBox3";
      this.textBox3.Size = new System.Drawing.Size(208, 20);
      this.textBox3.TabIndex = 2;
      this.textBox3.Text = "";
      // 
      // label4
      // 
      this.label4.Location = new System.Drawing.Point(16, 163);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(120, 16);
      this.label4.TabIndex = 11;
      this.label4.Text = "City";
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(16, 32);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(160, 20);
      this.textBox1.TabIndex = 0;
      this.textBox1.Text = "";
      // 
      // textBox6
      // 
      this.textBox6.Location = new System.Drawing.Point(224, 184);
      this.textBox6.Name = "textBox6";
      this.textBox6.Size = new System.Drawing.Size(48, 20);
      this.textBox6.TabIndex = 5;
      this.textBox6.Text = "";
      // 
      // EmpForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.button2;
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button2,
                                      this.button1,
                                      this.label6,
                                      this.label5,
                                      this.label4,
                                      this.label3,
                                      this.label2,
                                      this.label1,
                                      this.textBox6,
                                      this.textBox5,
                                      this.textBox4,
                                      this.textBox3,
                                      this.textBox2,
                                      this.textBox1});
      this.MinimizeBox = false;
      this.Name = "EmpForm";
      this.ShowInTaskbar = false;
      this.Text = "EmpForm";
      this.ResumeLayout(false);
    }
    #endregion
    public void GetEmployeeData (out EMPLOYEE emp)
    {
      emp.FirstName = textBox1.Text;
      emp.LastName = textBox2.Text;
      emp.Address = textBox3.Text;
      emp.City = textBox4.Text;
      emp.State = textBox5.Text;
      emp.Zip = textBox6.Text;
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
      this.Close ();
    }
  }
  
}


Form hide

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
  public class Form2 : Form
  {
    private System.Windows.Forms.Button Button3;
    private System.Windows.Forms.Button Button2;
    private System.Windows.Forms.Button Button1;
      
      
    public Form2()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.Button3 = new System.Windows.Forms.Button();
      this.Button2 = new System.Windows.Forms.Button();
      this.Button1 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // Button3
      // 
      this.Button3.Location = new System.Drawing.Point(120, 56);
      this.Button3.Name = "Button3";
      this.Button3.Size = new System.Drawing.Size(88, 32);
      this.Button3.TabIndex = 8;
      this.Button3.Text = "Become Child of Parent2";
      this.Button3.Click += new System.EventHandler(this.Button3_Click);
      // 
      // Button2
      // 
      this.Button2.Location = new System.Drawing.Point(12, 56);
      this.Button2.Name = "Button2";
      this.Button2.Size = new System.Drawing.Size(88, 32);
      this.Button2.TabIndex = 7;
      this.Button2.Text = "Become Child of Parent1";
      this.Button2.Click += new System.EventHandler(this.Button2_Click);
      // 
      // Button1
      // 
      this.Button1.Location = new System.Drawing.Point(12, 12);
      this.Button1.Name = "Button1";
      this.Button1.Size = new System.Drawing.Size(88, 32);
      this.Button1.TabIndex = 6;
      this.Button1.Text = "Become Parent";
      this.Button1.Click += new System.EventHandler(this.Button1_Click);
      // 
      // Form2
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(292, 154);
      this.Controls.Add(this.Button3);
      this.Controls.Add(this.Button2);
      this.Controls.Add(this.Button1);
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.Name = "Form2";
      this.Text = "Form2";
      this.ResumeLayout(false);
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
      this.Hide();
      this.MdiParent = null;
      this.IsMdiContainer = true;
      this.Show();
    }
    private void Button2_Click(object sender, System.EventArgs e)
    {
      this.Hide();
      this.IsMdiContainer = false;
      this.MdiParent = Program.Main2;
      this.Show();
    }
    private void Button3_Click(object sender, System.EventArgs e)
    {
      this.Hide();
      this.IsMdiContainer = false;
      this.MdiParent = Program.Main1;
      this.Show();
    }
  }
  public class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
      Application.Exit();
    }
    private void InitializeComponent()
    {
      this.SuspendLayout();
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(422, 351);
      this.IsMdiContainer = true;
      this.Name = "Form1";
      this.Text = "Form1";
      this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
      this.ResumeLayout(false);
    }
  }
  static class Program
  {
    public static Form1 Main1 = new Form1();
    public static Form1 Main2 = new Form1();
    public static Form2 Child = new Form2();
    [STAThread]
    public static void Main()
    {
      Application.EnableVisualStyles();
      Main1.Text = "Parent 2";
      Main2.Text = "Parent 1";
      Main1.Show();
      Main2.Show();
      Child.MdiParent = Main2;
      Child.Show();
      System.Windows.Forms.Application.Run();
    }
  }


FormStartPosition.CenterScreen

 

using System.Drawing;
using System.Windows.Forms;
   
class FormProperties
{
     public static void Main()
     {
          Form form = new Form();
   
          form.Text            = "Form Properties";
          form.BackColor       = Color.BlanchedAlmond;
          form.Width          *= 2;
          form.Height         /= 2;
   
          form.FormBorderStyle = FormBorderStyle.FixedSingle;
          form.MaximizeBox     = false;
          form.Cursor          = Cursors.Hand;
          form.StartPosition   = FormStartPosition.CenterScreen;
   
          Application.Run(form);
     }
}


Login from

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.ruponentModel;
using System.Windows.Forms;
namespace Wrox.ProWinProg.Chapter01.WinFormNonVS
{
   public class frmLogin : System.Windows.Forms.Form
   {
      System.Windows.Forms.TextBox txtUser;
      System.Windows.Forms.Button btnOK;
      System.Windows.Forms.Button btnCancel;
      public frmLogin()
      {
         txtUser = new System.Windows.Forms.TextBox();
         txtUser.Location = new Point(30, 15);
         txtUser.Size = new Size(250, 20);
         txtUser.Text = "";
         txtUser.Name = "txtUser";
         this.Controls.Add(txtUser);
         btnOK = new System.Windows.Forms.Button();
         btnOK.Location = new Point(40,
            (txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
         btnOK.Text="OK";
         btnOK.Name="btnOK";
         this.Controls.Add(btnOK);
         btnCancel = new System.Windows.Forms.Button();
         btnCancel.Location = new Point((this.Size.Width -
                                         btnCancel.Size.Width) - 40,
            (txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
         btnCancel.Text = "Cancel";
         btnCancel.Name = "btnCancel";
         this.Controls.Add(btnCancel);
         this.Size = new Size(this.Size.Width, btnCancel.Location.Y +
                              btnCancel.Size.Height + 60);
         btnCancel.Click += new System.EventHandler(btnCancelHandler);
         btnOK.Click += new System.EventHandler(btnEventHandler);
      }
      private void btnEventHandler(object sender, System.EventArgs e)
      {
         MessageBox.Show(((Button)sender).Name);
      }
   
      private void btnCancelHandler(object sender, System.EventArgs e)
      {
         MessageBox.Show("The second handler");
      }
      [STAThread]
      static void Main()
      {
         Application.Run(new frmLogin());
      }
   }
}

<A href="http://www.nfex.ru/Code/CSharpDownload/WinFormVS.zip">WinFormVS.zip( 19 k)</a>


makes a new window out of a graphics path that has been traced on this forms space

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace CustomWindow
{
    /// <summary>
    /// This project makes a new window out of a graphics path that has been traced on this forms space.
    /// Features include: Smoothing capability via turning sub paths into curves, etc
    /// Use point for stretching curve.
    /// 
    /// Have some kind of window that allows the user to set or view smoothing factor.
    /// The user can then see the shape in the smoother form side-by-side with the original.
    /// The user can then choose which one to make the new window with.
    /// Show the smoothing factor as percentage reduction in number of points = 1-(p1/p2)
    /// Have user choose some basic aspects of the form.
    ///   min and max buttons
    ///   border style
    ///   inflation size
    ///     have another form that has a slider so the user can graphically size the shape
    ///   opacity
    ///   background color
    ///   background image
    ///   Quit button on form
    /// </summary>
    public class CustomWindow : System.Windows.Forms.Form
    {
    #region Class Local Storage
    private GraphicsPath m_WindowPath;
    private GraphicsPath m_path;
    private Point m_StartPoint;
    private Point m_LastPoint;
    private bool  m_Drawing;
    private Rectangle InvalidRect;
    private Cursor DrawCursor;
    #endregion
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.MenuItem mnuExit;
    private System.Windows.Forms.MenuItem menuItem3;
    private System.Windows.Forms.MenuItem menuItem4;
    private System.Windows.Forms.MenuItem menuItem5;
    private System.Windows.Forms.MenuItem menuItem2;
    private System.Windows.Forms.MenuItem menuItem6;
    private System.Windows.Forms.MenuItem menuItem7;
    private System.Windows.Forms.MenuItem menuItem8;
    private System.Windows.Forms.MenuItem menuItem9;
    private System.Windows.Forms.TreeView PathTree;
    private System.Windows.Forms.Panel P1;
    private System.Windows.Forms.Splitter TreeSplitter;
    private System.Windows.Forms.MenuItem menuItem10;
    private System.Windows.Forms.Splitter PanelSplitter;
    private System.Windows.Forms.RichTextBox DebugWindow;


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public CustomWindow()
        {
            InitializeComponent();
      this.Icon = new Icon("Icon.ico");
      DrawCursor = new Cursor("Pen.cur");
      this.Size = new Size(800, 600);
      this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
      this.SetStyle(ControlStyles.DoubleBuffer,true);
//      this.MouseDown  += new MouseEventHandler(this.M_Down);
//      this.MouseUp    += new MouseEventHandler(this.M_Up);
//      this.MouseMove  += new MouseEventHandler(this.M_Move);
      P1.Paint      += new PaintEventHandler(this.PanelPaint);
      P1.MouseDown  += new MouseEventHandler(this.M_Down);
      P1.MouseUp    += new MouseEventHandler(this.M_Up);
      P1.MouseMove  += new MouseEventHandler(this.M_Move);
      m_WindowPath = new GraphicsPath();
      m_path = new GraphicsPath();
      InvalidRect = Rectangle.Empty;
      //Set RichTextBox properties
      DebugWindow.Height = this.Height /8;
      DebugWindow.Dock = DockStyle.Bottom;
      // Set properties of TreeView control.
      PathTree.Dock = DockStyle.Left;
      PathTree.Width = this.ClientSize.Width / 6;
      PathTree.TabIndex = 0;
      PathTree.Nodes.Add("Shapes");
      //Set Panel Properties
      P1.Dock = DockStyle.Fill;
      P1.BackColor = Color.Bisque;

      //Set up the splitters
      TreeSplitter.Location = new Point(PathTree.Width, 0);
      TreeSplitter.Size = new Size(2, this.Height);
      TreeSplitter.BackColor = Color.Black;
      PanelSplitter.Dock = DockStyle.Bottom;
      PanelSplitter.Height = 2;
      PanelSplitter.BackColor = Color.Black;
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #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.mainMenu1 = new System.Windows.Forms.MainMenu();
      this.menuItem1 = new System.Windows.Forms.MenuItem();
      this.mnuExit = new System.Windows.Forms.MenuItem();
      this.menuItem3 = new System.Windows.Forms.MenuItem();
      this.menuItem4 = new System.Windows.Forms.MenuItem();
      this.menuItem5 = new System.Windows.Forms.MenuItem();
      this.menuItem2 = new System.Windows.Forms.MenuItem();
      this.menuItem6 = new System.Windows.Forms.MenuItem();
      this.menuItem7 = new System.Windows.Forms.MenuItem();
      this.menuItem8 = new System.Windows.Forms.MenuItem();
      this.menuItem9 = new System.Windows.Forms.MenuItem();
      this.PathTree = new System.Windows.Forms.TreeView();
      this.P1 = new System.Windows.Forms.Panel();
      this.TreeSplitter = new System.Windows.Forms.Splitter();
      this.menuItem10 = new System.Windows.Forms.MenuItem();
      this.DebugWindow = new System.Windows.Forms.RichTextBox();
      this.PanelSplitter = new System.Windows.Forms.Splitter();
      this.SuspendLayout();
      // 
      // mainMenu1
      // 
      this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                              this.menuItem1,
                                                                              this.menuItem3,
                                                                              this.menuItem7});
      // 
      // menuItem1
      // 
      this.menuItem1.Index = 0;
      this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                              this.mnuExit});
      this.menuItem1.Text = "File";
      // 
      // mnuExit
      // 
      this.mnuExit.Index = 0;
      this.mnuExit.Text = "&Exit";
      this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
      // 
      // menuItem3
      // 
      this.menuItem3.Index = 1;
      this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                              this.menuItem4,
                                                                              this.menuItem10,
                                                                              this.menuItem5,
                                                                              this.menuItem2,
                                                                              this.menuItem6,
                                                                              this.menuItem9});
      this.menuItem3.Text = "Shape";
      // 
      // menuItem4
      // 
      this.menuItem4.Index = 0;
      this.menuItem4.Text = "Create";
      // 
      // menuItem5
      // 
      this.menuItem5.Index = 2;
      this.menuItem5.Text = "Resize";
      // 
      // menuItem2
      // 
      this.menuItem2.Index = 3;
      this.menuItem2.Text = "-";
      // 
      // menuItem6
      // 
      this.menuItem6.Index = 4;
      this.menuItem6.Text = "Load Shape";
      // 
      // menuItem7
      // 
      this.menuItem7.Index = 2;
      this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                              this.menuItem8});
      this.menuItem7.Text = "Form";
      // 
      // menuItem8
      // 
      this.menuItem8.Index = 0;
      this.menuItem8.Text = "Spawn";
      // 
      // menuItem9
      // 
      this.menuItem9.Index = 5;
      this.menuItem9.Text = "Save Shape";
      // 
      // PathTree
      // 
      this.PathTree.ImageIndex = -1;
      this.PathTree.Name = "PathTree";
      this.PathTree.SelectedImageIndex = -1;
      this.PathTree.Size = new System.Drawing.Size(88, 488);
      this.PathTree.TabIndex = 0;
      // 
      // P1
      // 
      this.P1.Location = new System.Drawing.Point(112, 16);
      this.P1.Name = "P1";
      this.P1.Size = new System.Drawing.Size(464, 472);
      this.P1.TabIndex = 1;
      // 
      // TreeSplitter
      // 
      this.TreeSplitter.Name = "TreeSplitter";
      this.TreeSplitter.Size = new System.Drawing.Size(8, 573);
      this.TreeSplitter.TabIndex = 2;
      this.TreeSplitter.TabStop = false;
      // 
      // menuItem10
      // 
      this.menuItem10.Index = 1;
      this.menuItem10.Text = "Smooth";
      // 
      // DebugWindow
      // 
      this.DebugWindow.Location = new System.Drawing.Point(24, 520);
      this.DebugWindow.Name = "DebugWindow";
      this.DebugWindow.Size = new System.Drawing.Size(536, 40);
      this.DebugWindow.TabIndex = 3;
      this.DebugWindow.Text = "richTextBox1";
      // 
      // PanelSplitter
      // 
      this.PanelSplitter.Location = new System.Drawing.Point(8, 0);
      this.PanelSplitter.Name = "PanelSplitter";
      this.PanelSplitter.Size = new System.Drawing.Size(8, 573);
      this.PanelSplitter.TabIndex = 4;
      this.PanelSplitter.TabStop = false;
      // 
      // CustomWindow
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(592, 573);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.PanelSplitter,
                                                                  this.DebugWindow,
                                                                  this.TreeSplitter,
                                                                  this.P1,
                                                                  this.PathTree});
      this.Menu = this.mainMenu1;
      this.Name = "CustomWindow";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "CustomWindow";
      this.Load += new System.EventHandler(this.CustomWindow_Load);
      this.ResumeLayout(false);
    }
        #endregion
    #region Main Entry Point
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new CustomWindow());
        }
    #endregion
    private void CustomWindow_Load(object sender, System.EventArgs e)
    {
    }
//    protected override void OnPaint(PaintEventArgs e)
//    {
//      Graphics G = e.Graphics;
//      PaintMe(e.Graphics);
//    }
    private void PanelPaint(object sender, PaintEventArgs e)
    {
      PaintMe(e.Graphics);
    }
    private void PaintMe(Graphics G)
    {
      G.SmoothingMode = SmoothingMode.AntiAlias;
      if (m_WindowPath.PointCount > 0)
        G.DrawPath(Pens.Black, m_WindowPath);
      if (m_path.PointCount > 0)
        G.DrawPath(Pens.Red, m_path);
    }
    private void M_Down(object sender, MouseEventArgs m)
    {
      m_StartPoint  = new Point(m.X, m.Y);
      m_LastPoint   = m_StartPoint;
      m_WindowPath  = new GraphicsPath();
      m_Drawing     = true;
      P1.Invalidate();
    }
    private void M_Up(object sender, MouseEventArgs m)
    {
      m_WindowPath.CloseFigure();
      m_Drawing     = false;
      //Smooth out the path by reducing lines that make up path
      m_path = SmootherPath(m_WindowPath);
      Matrix Q = new Matrix();
      Q.Translate(50, 5);
      m_path.Transform(Q);
      int a = m_WindowPath.PointCount;
      int b = m_path.PointCount;
      //Draw the paths and make a window.
      P1.Invalidate();
      MakeWindow();
    }
    private void M_Move(object sender, MouseEventArgs m)
    {
      if(m_Drawing)
      {
        m_WindowPath.AddLine(m_LastPoint.X, m_LastPoint.Y, m.X, m.Y);
        m_LastPoint.X = m.X;
        m_LastPoint.Y = m.Y;
        InvalidRect = Rectangle.Truncate(m_WindowPath.GetBounds());
        InvalidRect.Inflate( new Size(2, 2) );
        P1.Invalidate(InvalidRect);
      }
    }
    /// <doc>
    /// Start first with reducing the number of lines in this graphics path
    /// 1. read first point
    /// 2. if x value of next point = x value of last point then skip
    /// 3. if x value of next point != value of last point then...
    /// 3a. make line based on these two points
    /// 3b. add line to new path
    /// 3c. first point = next point 
    /// 4. repeat 1-3c
    /// 5. Repeat 1-4 for both X and Y
    /// </doc>
    private GraphicsPath SmootherPath(GraphicsPath gp)
    {
      PathData pd = gp.PathData;
      PointF pt1 = new Point(-1,-1);
      //First do all values in the X range
      GraphicsPath FixedPath_X = new GraphicsPath();
      foreach (PointF p in pd.Points)
      {
        if (pt1.X == -1)
        {
          pt1 = p;
          continue;
        }
        // If I introduced an error factor here I could smooth it out even more
        if (p.X != pt1.X)
        {
          FixedPath_X.AddLine(pt1, p);
          pt1 = p;
        }
      }
      FixedPath_X.CloseFigure();
      //Second do all values in the Y range
      pd = FixedPath_X.PathData;
      pt1 = new Point(-1,-1);
      GraphicsPath FixedPath_Y = new GraphicsPath();
      foreach (PointF p in pd.Points)
      {
        if (pt1.Y == -1)
        {
          pt1 = p;
          continue;
        }
        // If I introduced an error factor here I could smooth it out even more
        if (p.Y != pt1.Y)
        {
          FixedPath_Y.AddLine(pt1, p);
          pt1 = p;
        }
      }
      FixedPath_Y.CloseFigure();
      return FixedPath_Y;
    }
    private void MakeWindow()
    {
      Form frm = new Form();
      GraphicsPath path = (GraphicsPath)m_WindowPath.Clone();
      Matrix Xlate = new Matrix();
      //Find the lowest Y value and normalize all Y values to zero
      //Find the lowest X value and normalize all X values to zero
      PointF[] p = path.PathPoints;
      int Xoffset = 9999;
      int Yoffset = 9999;
      foreach (PointF p2 in p)
      {
        if (p2.X < Xoffset)
          Xoffset = (int)p2.X;
        if (p2.Y < Yoffset)
          Yoffset = (int)p2.Y;
      }
      Xlate.Translate(-Xoffset, -Yoffset);
      path.Transform(Xlate);
     
      // Set the paractical viewing region of the form
      frm.Region = new Region(path);
      //Set the size of the form
      Rectangle frmRect = Rectangle.Truncate(path.GetBounds());
      frm.Size = frmRect.Size;
      //Set some other parameters
      frm.StartPosition = FormStartPosition.CenterParent;
      frm.FormBorderStyle = FormBorderStyle.FixedSingle;
      frm.ShowDialog();
      frm.Dispose();
      Xlate.Dispose();
    }
    private void mnuExit_Click(object sender, System.EventArgs e)
    {
      this.Close();
    }
  }
}

<A href="http://www.nfex.ru/Code/CSharpDownload/CustomWindow.zip">CustomWindow.zip( 34 k)</a>


Print out Form size and position related information

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class FormSize: Form
{
     public static void Main()
     {
          Application.Run(new FormSize());
     }
     public FormSize()
     {
          BackColor = Color.White;
     }
     protected override void OnMove(EventArgs ea)
     {
          Invalidate();
     }
     protected override void OnResize(EventArgs ea)
     {
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          string   str  = "Location: "        + Location        + "\n"   +
                          "Size: "            + Size            + "\n"   +
                          "Bounds: "          + Bounds          + "\n"   +
                          "Width: "           + Width           + "\n"   +
                          "Height: "          + Height          + "\n"   +
                          "Left: "            + Left            + "\n"   +
                          "Top: "             + Top             + "\n"   +
                          "Right: "           + Right           + "\n"   +
                          "Bottom: "          + Bottom          + "\n\n" +
                          "DesktopLocation: " + DesktopLocation + "\n"   +
                          "DesktopBounds: "   + DesktopBounds   + "\n\n" +
                          "ClientSize: "      + ClientSize      + "\n"   +
                          "ClientRectangle: " + ClientRectangle;
   
          graphics.DrawString(str, Font, Brushes.Black, 0, 0);
    }
}


Set ClientSize to change the form window size

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class AutoScaleDemo: Form
{
     public static void Main()
     {
          Application.Run(new AutoScaleDemo());
     }
     public AutoScaleDemo()
     {
          Font = new Font("Arial", 12);
          FormBorderStyle = FormBorderStyle.FixedSingle;
   
          int[] aiPointSize = { 8, 12, 16, 24, 32 };
   
          for (int i = 0; i < aiPointSize.Length; i++)
          {
               Button btn    = new Button();
               btn.Parent    = this;
               btn.Text      = "Use " + aiPointSize[i] + "-point font";
               btn.Tag       = aiPointSize[i];
               btn.Location  = new Point(4, 16 + 24 * i);
               btn.Size      = new Size(80, 16);
               btn.Click    += new EventHandler(ButtonOnClick);
          }
          ClientSize = new Size(88, 16 + 24 * aiPointSize.Length);
          AutoScaleBaseSize = new Size(4, 8);
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          pea.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0);
     }
     void ButtonOnClick(object obj, EventArgs ea)
     {
          Button btn = (Button) obj;
   
          SizeF sizefOld = GetAutoScaleSize(Font);
          Font = new Font(Font.FontFamily, (int) btn.Tag);
          SizeF sizefNew = GetAutoScaleSize(Font);
   
          Scale(sizefNew.Width  / sizefOld.Width,sizefNew.Height / sizefOld.Height);
     }
}


Set Form window title and center the Form window on the desktop

using System;
using System.Windows.Forms;
using System.Drawing;
public class TestForm2 {
  static void Main() {
    Form simpleForm = new Form();
    simpleForm.Height = 200;
    simpleForm.Width = 200;
    simpleForm.Text = "This is the title";
    // Center the Form on the desktop
    simpleForm.StartPosition = FormStartPosition.CenterScreen;
    simpleForm.Visible = true;
    Application.Run(simpleForm);
  }
}


Set ResizeRedraw property

 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class RandomClearResizeRedraw: Form
{
     public static void Main()      
     {
          Application.Run(new RandomClearResizeRedraw());
     }
     public RandomClearResizeRedraw()      
     {
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          Random   rand = new Random();
   
          graphics.Clear(Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
     }
}


Simple form

/*
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 FormSample
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class FormSample : System.Windows.Forms.Form
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public FormSample()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #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()
    {
      // 
      // FormSample
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FormSample());
    }
  }
}


Simplest form code: set window title

public class MyForm : System.Windows.Forms.Form{
  public MyForm(){
    Text = "Hello World";
  }
  public static void Main() {
    System.Windows.Forms.Application.Run(new MyForm());
  }
}


Use Font from Form to paint string on a form

 
using System;
using System.Drawing;
using System.Windows.Forms;
class InheritHelloWorld : Form {
    public static void Main() {
        Application.Run(new InheritHelloWorld());
    }
    public InheritHelloWorld() {
        Text = "Inherit " + Text;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        Graphics graphics = pea.Graphics;
        graphics.DrawString("Hello from InheritHelloWorld!",
                        Font, Brushes.Black, 0, 100);
    }
}


Windows Forms Getting Started

namespace DiskDiff
{
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ruponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    public class DiskDiff : System.Windows.Forms.Form
    {
        private System.ruponentModel.Container components;
        
        public DiskDiff()
        {
            InitializeComponent();
        }
        
        private void InitializeComponent()
        {
            this.ruponents = new System.ruponentModel.Container();
            this.Size = new System.Drawing.Size(300,300);
            this.Text = "Form1";
        }
        
        public static void Main(string[] args) 
        {
            Application.Run(new DiskDiff());
        }
    }
}