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

    


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); } } [/csharp]

Form Mouse down action


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

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.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
      Console.WriteLine("Mouse down");
   }

}



           
          


Form resize and redraw


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
        private System.Windows.Forms.CheckBox chkResizeRedraw;
      public Form1() {
            InitializeComponent();
            
      }
    private void FlawedResizing_Paint(object sender, PaintEventArgs e)
    {
            ResizeRedraw = chkResizeRedraw.Checked;
            Pen pen = new Pen(Color.Red, 1);
            e.Graphics.DrawEllipse(pen, new Rectangle(new Point(0, 0),
              this.ClientSize));
            pen.Dispose();        
            
    }

    private void chkResizeRedraw_CheckedChanged(object sender, EventArgs e)
    {
      Invalidate();
    }
    private void InitializeComponent()
    {
            this.chkResizeRedraw = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // chkResizeRedraw
            // 
            this.chkResizeRedraw.AutoSize = true;
            this.chkResizeRedraw.Location = new System.Drawing.Point(102, 104);
            this.chkResizeRedraw.Name = "chkResizeRedraw";
            this.chkResizeRedraw.Size = new System.Drawing.Size(95, 17);
            this.chkResizeRedraw.TabIndex = 0;
            this.chkResizeRedraw.Text = "ResizeRedraw";
            this.chkResizeRedraw.CheckedChanged += new System.EventHandler(this.chkResizeRedraw_CheckedChanged);
            // 
            // FlawedResizing
            // 
            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.chkResizeRedraw);
            this.Name = "FlawedResizing";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.Text = "FlawedResizing";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.FlawedResizing_Paint);
            this.ResumeLayout(false);
            this.PerformLayout();

    }

      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }

}


           
          


Form window load event


   

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  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.Load+=new EventHandler(Form_Load);


      myButton = new Button();
      myButton.Text = "www.kutayzorlu.com/java2s/com";
      myButton.Location = new System.Drawing.Point(64, 32);
      myButton.Size = new System.Drawing.Size(150, 50);

      Controls.Add(myButton);
      CenterToScreen();
    }
        public void Form_Load(object sender,EventArgs eArgs)
        {
            MessageBox.Show("Loading Form.....");
        }

    static void Main() 
    {
      Application.Run(new Form1());
    }
  }


           
          


Bind key action to a form window


   


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

public class KeyReader : System.Windows.Forms.Form {
    private System.Windows.Forms.Label lblPress;
    private System.Windows.Forms.Label lblDown;
    private System.Windows.Forms.Label label1;

    public KeyReader() {
       InitializeComponent();
    }
     private void InitializeComponent() {
      this.lblPress = new System.Windows.Forms.Label();
      this.lblDown = new System.Windows.Forms.Label();
      this.label1 = new System.Windows.Forms.Label();
      this.SuspendLayout();

      this.lblPress.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.lblPress.Location = new System.Drawing.Point(8, 190);
      this.lblPress.Name = "lblPress";
      this.lblPress.Size = new System.Drawing.Size(408, 48);
      this.lblPress.TabIndex = 0;
      this.lblPress.Text = "Press:";
      this.lblPress.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

      this.lblDown.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.lblDown.Location = new System.Drawing.Point(8, 254);
      this.lblDown.Name = "lblDown";
      this.lblDown.Size = new System.Drawing.Size(408, 48);
      this.lblDown.TabIndex = 0;
      this.lblDown.Text = "Down:";
      this.lblDown.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;


      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(424, 365);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.label1,
                                                                  this.lblDown,
                                                                  this.lblPress});
      this.KeyPreview = true;
      this.Name = "KeyReader";
      this.Text = "KeyReader";
      this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyReader_KeyDown);
      this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KeyReader_KeyPress);
      this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
       Application.Run(new KeyReader());
    }
    private void KeyReader_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
      lblPress.Text =  "Press: " + Convert.ToString(e.KeyChar);
    }
    private void KeyReader_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
      lblDown.Text = "Down: " + Convert.ToString(e.KeyCode);
      if (e.KeyCode == Keys.ShiftKey){
         MessageBox.Show("That is one shifty character");
      }
    } 
}

           
          


Form OnResize

   
 


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 OnResize(EventArgs ea)
     {
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          string   str  = "Location: "        + Location        + "
"   +
                          "ClientRectangle: " + ClientRectangle;
   
          graphics.DrawString(str, Font, Brushes.Black, 0, 0);
    }
}