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 OnMove event

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class FormSize: Form
{
     public static void Main()
     {
          Application.Run(new FormSize());
     }
     public FormSize()
     {
          Text = "Form Size";
          BackColor = Color.White;
     }
     protected override void OnMove(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);
    }
}

    


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

    


OnClick event

   
 

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
   
class PrintableForm: Form
{
     public static void Main()
     {
          Application.Run(new PrintableForm());
     }
     public PrintableForm()
     {
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }
     protected override void OnClick(EventArgs ea)
     {
          PrintDocument prndoc = new PrintDocument();
   
          prndoc.DocumentName = Text;
          prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
          prndoc.Print();
     }
     void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
     {
          Graphics grfx  = ppea.Graphics;
          SizeF    sizef = grfx.VisibleClipBounds.Size;
   
          DoPage(grfx, Color.Black, (int)sizef.Width, (int)sizef.Height);
     }
     protected virtual void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Pen pen = new Pen(clr);
   
          grfx.DrawLine(pen, 0,      0, cx - 1, cy - 1);
          grfx.DrawLine(pen, cx - 1, 0, 0,      cy - 1);
     }
}