OnKeyDown event

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class ExitOnX: Form
{
     public static void Main()
     {
          Application.Run(new ExitOnX());
     }
     public ExitOnX()
     {
          Text = "Exit on X";
     }
     protected override void OnKeyDown(KeyEventArgs kea)
     {
          if (kea.KeyCode == Keys.X)
               Close();
     }
}

    


On Mouse Wheel

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class PoePoem: Form
{
     public static void Main()
     {
          if (!SystemInformation.MouseWheelPresent)
          {
               Console.WriteLine("Program needs a mouse with a mouse wheel!");
               return;
          }
          Application.Run(new PoePoem());
     }
     public PoePoem()
     {

     }
     protected override void OnMouseWheel(MouseEventArgs mea)
     {
          Console.WriteLine(mea.Delta);
   
     }
}

    


OnMouseEnter, OnMouseHover, OnMouseLeave event

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class EnterLeave: Form
{
     bool bInside = false;
   
     public static void Main()
     {
          Application.Run(new EnterLeave());
     }
     public EnterLeave()
     {
     }
     protected override void OnMouseEnter(EventArgs ea)
     {
          bInside = true;
          Invalidate();
     }
     protected override void OnMouseLeave(EventArgs ea)
     {
          bInside = false;
          Invalidate();
     }
     protected override void OnMouseHover(EventArgs ea)
     {
          Graphics grfx = CreateGraphics();
    
          grfx.Clear(Color.Red);
          System.Threading.Thread.Sleep(500);
          grfx.Clear(Color.Green);
          grfx.Dispose();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
   
          grfx.Clear(bInside ? Color.Green : BackColor);
     }
}

    


Scrolling (AutoScrollMinSize)

image_pdfimage_print
   
 


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 {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g;
        g = Graphics.FromHwnd(this.Handle);
        GraphicsUnit units = GraphicsUnit.Pixel;

        string path = "your.jpg";
        Image im = Image.FromFile(path);
        this.AutoScrollMinSize = new Size(im.Width, im.Height);
        //this.AutoScroll = true;

        Point P = this.AutoScrollPosition;
        Rectangle dstR = this.ClientRectangle;
        RectangleF srcR = new RectangleF(-P.X, -P.Y, dstR.Width, dstR.Height);
        g.DrawImage(im, dstR, srcR, units);
        g.Dispose();
    }
}

    


OnInputLanguageChanged

image_pdfimage_print


   
 

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

public class InternationalText : System.Windows.Forms.Form {
    internal System.Windows.Forms.Label lblInternationalText;
    internal System.Windows.Forms.Label lblCharCode;
    private System.Windows.Forms.TextBox textBox1;
    public InternationalText() {
        this.lblInternationalText = new System.Windows.Forms.Label();
        this.lblCharCode = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();

        this.lblInternationalText.Location = new System.Drawing.Point(8, 64);
        this.lblInternationalText.Size = new System.Drawing.Size(288, 23);

        this.lblCharCode.Location = new System.Drawing.Point(8, 96);
        this.lblCharCode.Size = new System.Drawing.Size(88, 23);

        this.textBox1.Location = new System.Drawing.Point(8, 24);
        this.textBox1.Size = new System.Drawing.Size(288, 20);
        this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(304, 134);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.textBox1,
                                                                      this.lblCharCode,
                                                                      this.lblInternationalText});
        this.MaximizeBox = false;
        this.ResumeLayout(false);

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

    protected override void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
        MessageBox.Show(e.InputLanguage.Culture.Name);
    }

    protected override void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
        MessageBox.Show(e.InputLanguage.Culture.Name);
    }

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
        lblInternationalText.Text += e.KeyChar.ToString();
        lblCharCode.Text = ((int)e.KeyChar).ToString();
    }
}

    


Form Focus event

image_pdfimage_print


   
 

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

public class FocusForm : System.Windows.Forms.Form {
    private System.Windows.Forms.TextBox txtFocusForm;
    private System.Windows.Forms.Button btFocusForm;
    public FocusForm() {
        this.txtFocusForm = new System.Windows.Forms.TextBox();
        this.btFocusForm = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.txtFocusForm.Location = new System.Drawing.Point(8, 8);
        this.txtFocusForm.Size = new System.Drawing.Size(336, 20);
        this.txtFocusForm.LostFocus += new System.EventHandler(this.txtFocusForm_LostFocus);
        this.txtFocusForm.GotFocus += new System.EventHandler(this.txtFocusForm_GotFocus);

        this.btFocusForm.Location = new System.Drawing.Point(8, 40);
        this.btFocusForm.Size = new System.Drawing.Size(336, 23);
        this.btFocusForm.Click += new System.EventHandler(this.btFocusForm_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(352, 70);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.btFocusForm,
                                      this.txtFocusForm});
        this.ResumeLayout(false);
    }

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

    protected void txtFocusForm_LostFocus(object sender, EventArgs e) {
        Console.WriteLine("Goodbye!");
    }

    protected void txtFocusForm_GotFocus(object sender, EventArgs e) {
        Console.WriteLine("Hello!");
    }

    private void btFocusForm_Click(object sender, System.EventArgs e) {
        bool canFocus = txtFocusForm.CanFocus;
        bool containsFocus = this.ContainsFocus;
        bool focused = txtFocusForm.Focused;

        Console.WriteLine("Textbox can focus: " + canFocus +
                     "
Form children contain focus: " + containsFocus +
                     "
Textbox has focus: " + focused);

        txtFocusForm.Focus();
    }
}

    


Font list

image_pdfimage_print


   

/*
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.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FontList
{
  /// <summary>
  /// Summary description for FontListForm.
  /// </summary>
  public class FontListForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public FontListForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      InitForm ();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    FontFamily [] fonts;
    protected void InitForm ()
    {
      fonts = FontFamily.Families;
      foreach (FontFamily font in fonts)
        listBox1.Items.Add (font.Name);
    }
    /// <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.listBox1 = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
      this.listBox1.Location = new System.Drawing.Point(22, 21);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(226, 190);
      this.listBox1.TabIndex = 0;
      this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_OnDoubleClick);
      this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_OnMeasureItem);
      this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_OnDrawItem);
      this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_OnSelChanged);
      // 
      // FontListForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(266, 243);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.listBox1});
      this.Name = "FontListForm";
      this.Text = "Font Families";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FontListForm());
    }

    private void listBox1_OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
      Rectangle rc = e.Bounds;
      string strSample = "The quick red fox jumps over the lazy brown dog";
      Font fntText;
      Font fntSample;
      int cy = 3 * (rc.Bottom - rc.Top) / 4;
      try
      {
        fntText = new Font ("Times New Roman", 8, GraphicsUnit.Point);
      }
      catch (ArgumentException)
      {
        MessageBox.Show ("Cannot create Times New Roman font", "Pied font",
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
      }
      try
      {
        fntText = new Font ("Times New Roman", 8, GraphicsUnit.Point);
        fntSample = new Font (fonts[e.Index], 8, GraphicsUnit.Point);
      }
      catch (ArgumentException)
      {
        DialogResult mbResult = MessageBox.Show("Cannot open font " + fonts[e.Index].Name +"
Do you want to delete this item from the list","Pied Font Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        switch (mbResult)
        {
          case DialogResult.Yes:
            listBox1.Items.RemoveAt (e.Index);
            return;
          case DialogResult.No:
            break;
        }
        strSample = "Cannot open font " + fonts[e.Index].Name;
        fntSample = fntText;
      }
      Brush brush;
      string str = fonts[e.Index].Name + " -- ";
      if ((e.State &amp; DrawItemState.Selected) != 0)
      {
        e.Graphics.FillRectangle (Brushes.DarkBlue, rc);
        e.DrawFocusRectangle ();
        brush = Brushes.White;
      }
      else
      {
        e.Graphics.FillRectangle (Brushes.White, rc);
        brush = Brushes.Black;
      }
      e.Graphics.DrawString (str, fntText, brush, rc);
      StringFormat format = new StringFormat ();
      format.LineAlignment = StringAlignment.Center;
      format.FormatFlags = StringFormatFlags.LineLimit;
      SizeF size = e.Graphics.MeasureString (str, fntText);
      rc = new Rectangle (rc.Left + (int) (size.Width + .5),
        rc.Top, rc.Width, rc.Height);
      e.Graphics.DrawString (strSample, fntSample, brush, rc, format);
      fntText.Dispose ();
      fntSample.Dispose();
    }

    private void listBox1_OnSelChanged(object sender, System.EventArgs e)
    {
      listBox1.Invalidate ();
    }

    private void listBox1_OnMeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    {
      MeasureItemEventArgs args = e;
      Font plain = new Font ("Times New Roman", 10, GraphicsUnit.Point);
      SizeF size = e.Graphics.MeasureString ("Test", plain);
      args.ItemHeight = (int) (size.Height + .5);
      plain.Dispose ();
    }

    private void listBox1_OnDoubleClick(object sender, System.EventArgs e)
    {
      if (e is ListChangedEventArgs)
      {
        frmSampleText stuff = new frmSampleText ();
      }
    }
  }
  /// <summary>
  /// Summary description for frmSampleText.
  /// </summary>
  public class frmSampleText : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public frmSampleText()
    {
      //
      // 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.textBox1 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(8, 8);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(272, 136);
      this.textBox1.TabIndex = 0;
      this.textBox1.Text = "textBox1";
      // 
      // frmSampleText
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
      this.ClientSize = new System.Drawing.Size(296, 157);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.textBox1});
      this.Name = "frmSampleText";
      this.Text = "Sample Text";
      this.ResumeLayout(false);

    }
    #endregion
  }

}