Csharp/CSharp Tutorial/GUI Windows Forms/TextBox — различия между версиями

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

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

Count lines in the TextBox and read text in TextBox line by line

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void showLinesButton_Click(object sender, EventArgs e)
    {
        StringBuilder lineInfo = new StringBuilder();
        lineInfo.Append("There are " + multiLineBox.Lines.Length.ToString() + " lines.\n");
        foreach (string line in multiLineBox.Lines)
        {
            lineInfo.Append(line + "\n");
        }
        MessageBox.Show(lineInfo.ToString());
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.multiLineBox = new System.Windows.Forms.TextBox();
        this.showLinesButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // multiLineBox
        // 
        this.multiLineBox.Location = new System.Drawing.Point(12, 12);
        this.multiLineBox.Multiline = true;
        this.multiLineBox.Name = "multiLineBox";
        this.multiLineBox.Size = new System.Drawing.Size(268, 213);
        this.multiLineBox.TabIndex = 0;
        // 
        // showLinesButton
        // 
        this.showLinesButton.Location = new System.Drawing.Point(205, 231);
        this.showLinesButton.Name = "showLinesButton";
        this.showLinesButton.Size = new System.Drawing.Size(75, 23);
        this.showLinesButton.TabIndex = 1;
        this.showLinesButton.Text = "Show Lines";
        this.showLinesButton.Click += new System.EventHandler(this.showLinesButton_Click);
        // 
        // Form1
        // 
        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.showLinesButton);
        this.Controls.Add(this.multiLineBox);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.TextBox multiLineBox;
    private System.Windows.Forms.Button showLinesButton;
}
public class TextBoxLineCountText
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

Get input value from TextBox and display it vis a messagebox

using System;
using System.Windows.Forms;
class GetValueFromTextBox : Form
{
    private Label label1;
    private TextBox textBox1;
    private Button button1;
    public GetValueFromTextBox() 
    {
        this.label1 = new Label();
        this.textBox1 = new TextBox();
        this.button1 = new Button();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(16, 36);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(128, 16);
        this.label1.TabIndex = 0;
        this.label1.Text = "Please enter your name:";
        this.textBox1.Location = new System.Drawing.Point(152, 32);
        this.textBox1.Name = "textBox1";
        this.textBox1.TabIndex = 1;
        this.textBox1.Text = "";
        this.button1.Location = new System.Drawing.Point(109, 80);
        this.button1.Name = "button1";
        this.button1.TabIndex = 2;
        this.button1.Text = "Enter";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.ClientSize = new System.Drawing.Size(292, 126);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        this.Name = "form1";
        this.Text = "";
        this.ResumeLayout(false);    
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
        Console.WriteLine(textBox1.Text);
        MessageBox.Show(textBox1.Text);
    }
    [STAThread]
    public static void Main() 
    {
        Application.EnableVisualStyles();
        Application.Run(new GetValueFromTextBox());
    }
}

Multiline TextBox

using System;
using System.ruponentModel;
using System.Windows.Forms;
public class TextBoxMultiline : System.Windows.Forms.Form
{
    private System.ruponentModel.Container components = null;
  private System.Windows.Forms.Button btnPasswordDecoderRing;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox passwordBox;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox capsOnlyBox;
  private System.Windows.Forms.Button btnGetMultiLineText;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox multiLineBox;
    public TextBoxMultiline()
    {
        InitializeComponent();
    CenterToScreen();
    }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.capsOnlyBox = new System.Windows.Forms.TextBox();
    this.multiLineBox = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.passwordBox = new System.Windows.Forms.TextBox();
    this.btnGetMultiLineText = new System.Windows.Forms.Button();
    this.btnPasswordDecoderRing = new System.Windows.Forms.Button();
    this.label3 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // capsOnlyBox
    // 
    this.capsOnlyBox.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
    this.capsOnlyBox.Location = new System.Drawing.Point(14, 176);
    this.capsOnlyBox.Name = "capsOnlyBox";
    this.capsOnlyBox.Size = new System.Drawing.Size(120, 20);
    this.capsOnlyBox.TabIndex = 3;
    this.capsOnlyBox.Text = "";
    // 
    // multiLineBox
    // 
    this.multiLineBox.AcceptsReturn = true;
    this.multiLineBox.AcceptsTab = true;
    this.multiLineBox.Location = new System.Drawing.Point(152, 8);
    this.multiLineBox.Multiline = true;
    this.multiLineBox.Name = "multiLineBox";
    this.multiLineBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.multiLineBox.Size = new System.Drawing.Size(240, 104);
    this.multiLineBox.TabIndex = 0;
    this.multiLineBox.Text = "Type some stuff here (and hit the return and tab keys...)";
    // 
    // label1
    // 
    this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
    this.label1.Location = new System.Drawing.Point(8, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(136, 56);
    this.label1.TabIndex = 1;
    this.label1.Text = "A multiline textbox that accepts tabs and return keystrokes.";
    // 
    // label2
    // 
    this.label2.Font = new System.Drawing.Font("Comic Sans MS", 12F);
    this.label2.Location = new System.Drawing.Point(14, 144);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(106, 24);
    this.label2.TabIndex = 4;
    this.label2.Text = "Caps only!!";
    // 
    // passwordBox
    // 
    this.passwordBox.Location = new System.Drawing.Point(160, 176);
    this.passwordBox.Name = "passwordBox";
    this.passwordBox.PasswordChar = "$";
    this.passwordBox.Size = new System.Drawing.Size(232, 20);
    this.passwordBox.TabIndex = 5;
    this.passwordBox.Text = "COM seems so old fashion";
    // 
    // btnGetMultiLineText
    // 
    this.btnGetMultiLineText.Location = new System.Drawing.Point(13, 72);
    this.btnGetMultiLineText.Name = "btnGetMultiLineText";
    this.btnGetMultiLineText.Size = new System.Drawing.Size(120, 32);
    this.btnGetMultiLineText.TabIndex = 2;
    this.btnGetMultiLineText.Text = "Get Text";
    this.btnGetMultiLineText.Click += new System.EventHandler(this.btnGetMultiLineText_Click);
    // 
    // btnPasswordDecoderRing
    // 
    this.btnPasswordDecoderRing.Location = new System.Drawing.Point(280, 144);
    this.btnPasswordDecoderRing.Name = "btnPasswordDecoderRing";
    this.btnPasswordDecoderRing.Size = new System.Drawing.Size(112, 24);
    this.btnPasswordDecoderRing.TabIndex = 7;
    this.btnPasswordDecoderRing.Text = "Decode Password";
    this.btnPasswordDecoderRing.Click += new System.EventHandler(this.btnPasswordDecoderRing_Click);
    // 
    // label3
    // 
    this.label3.Font = new System.Drawing.Font("Comic Sans MS", 12F);
    this.label3.Location = new System.Drawing.Point(152, 144);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(120, 24);
    this.label3.TabIndex = 6;
    this.label3.Text = "Password Box";
    // 
    // TextBoxMultiline
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(408, 221);
    this.Controls.Add(this.btnPasswordDecoderRing);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.passwordBox);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.capsOnlyBox);
    this.Controls.Add(this.btnGetMultiLineText);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.multiLineBox);
    this.Name = "TextBoxMultiline";
    this.Text = "TextBox Types";
    this.ResumeLayout(false);
  }
  protected void btnPasswordDecoderRing_Click (object sender, System.EventArgs e)
  {
    MessageBox.Show(passwordBox.Text, "Your password is:");
  }
  protected void btnGetMultiLineText_Click (object sender, System.EventArgs e)
  {
    MessageBox.Show(multiLineBox.Text, "Here is your text");
  }
    public static void Main(string[] args) 
    {
        Application.Run(new TextBoxMultiline());
    }
}

Simple Editor

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

   public class SimpleEditorForm : Form
   {
      private string fileName = "Untitled";
      private string[] lines;
      private int linesPrinted;
      public SimpleEditorForm()
      {
         InitializeComponent();
         if (fileName != null)
         {
            this.fileName = "fileName";
            OpenFile();
         }        
      }
      private void OpenFile()
      {
         try
         {
            textBoxEdit.Clear();
            textBoxEdit.Text = File.ReadAllText(fileName);
         }
         catch (IOException ex)
         {
            MessageBox.Show(ex.Message, "Simple Editor",
               MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         }
      }
      private void OnFileNew(object sender, EventArgs e)
      {
         fileName = "Untitled";
         SetFormTitle();
         textBoxEdit.Clear();
      }
      private void OnFileOpen(object sender, EventArgs e)
      {
         if (dlgOpenFile.ShowDialog() == DialogResult.OK)
         {
            fileName = dlgOpenFile.FileName;
            SetFormTitle();
            OpenFile();
         }
      }
      private void SaveFile()
      {
         try
         {
            File.WriteAllText(fileName, textBoxEdit.Text);
         }
         catch (IOException ex)
         {
            MessageBox.Show(ex.Message, "Simple Editor",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         }
      }
      private void OnFileSave(object sender, EventArgs e)
      {
         if (fileName == "Untitled")
         {
            OnFileSaveAs(sender, e);
         }
         else
         {
            SaveFile();
         }
      }
      private void SetFormTitle()
      {
         FileInfo fileInfo = new FileInfo(fileName);
         Text = fileInfo.Name + " - Simple Editor";
      }
      private void OnFileSaveAs(object sender, EventArgs e)
      {
         if (dlgSaveFile.ShowDialog() == DialogResult.OK)
         {
            fileName = dlgSaveFile.FileName;
            SetFormTitle();
            SaveFile();
         }
      }
      private void OnPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      {
         int x = e.MarginBounds.Left;
         int y = e.MarginBounds.Top;
         while (linesPrinted < lines.Length)
         {
            e.Graphics.DrawString(lines[linesPrinted++],new Font("Arial", 10), Brushes.Black, x, y);
            y += 25;
            if (y >= e.MarginBounds.Bottom)
            {
               e.HasMorePages = true;
               return;
            }
         }
         linesPrinted = 0;
         e.HasMorePages = false;
      }
      private void OnFilePrint(object sender, EventArgs e)
      {
         try
         {
            if (textBoxEdit.SelectedText != "")
            {
               dlgPrint.AllowSelection = true;
            }
            if (dlgPrint.ShowDialog() == DialogResult.OK)
            {
               printDocument.Print();
            }
         }
         catch (InvalidPrinterException ex)
         {
            MessageBox.Show(ex.Message, "Simple Editor",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
      }
      private void OnBeginPrint(object sender, PrintEventArgs e)
      {
         char[] param = { "\n" };
         if (dlgPrint.PrinterSettings.PrintRange == PrintRange.Selection)
         {
            lines = textBoxEdit.SelectedText.Split(param);
         }
         else
         {
            lines = textBoxEdit.Text.Split(param);
         }
         int i = 0;
         char[] trimParam = { "\r" };
         foreach (string s in lines)
         {
            lines[i++] = s.TrimEnd(trimParam);
         }
      }
      private void OnEndPrint(object sender, PrintEventArgs e)
      {
         lines = null;
      }
      private void OnFilePageSetup(object sender, EventArgs e)
      {
         dlgPageSetup.ShowDialog();
      }
      private void OnFilePrintPreview(object sender, EventArgs e)
      {
         dlgPrintPreview.ShowDialog();
      }
      private void InitializeComponent()
      {
         this.textBoxEdit = new System.Windows.Forms.TextBox();
         this.mainMenu = new System.Windows.Forms.MenuStrip();
         this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
         this.dlgSaveFile = new System.Windows.Forms.SaveFileDialog();
         this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
         this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.printPreviewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.pageSetupToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
         this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
         this.printDocument = new System.Drawing.Printing.PrintDocument();
         this.dlgPageSetup = new System.Windows.Forms.PageSetupDialog();
         this.dlgPrint = new System.Windows.Forms.PrintDialog();
         this.dlgPrintPreview = new System.Windows.Forms.PrintPreviewDialog();
         this.printPreviewControl = new System.Windows.Forms.PrintPreviewControl();
         this.mainMenu.SuspendLayout();
         this.SuspendLayout();
         this.textBoxEdit.AcceptsReturn = true;
         this.textBoxEdit.AcceptsTab = true;
         this.textBoxEdit.Dock = System.Windows.Forms.DockStyle.Fill;
         this.textBoxEdit.Location = new System.Drawing.Point(0, 24);
         this.textBoxEdit.Multiline = true;
         this.textBoxEdit.Name = "textBoxEdit";
         this.textBoxEdit.ScrollBars = System.Windows.Forms.ScrollBars.Both;
         this.textBoxEdit.Size = new System.Drawing.Size(546, 203);
         this.mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {this.fileToolStripMenuItem});
         this.mainMenu.Location = new System.Drawing.Point(0, 0);
         this.mainMenu.Size = new System.Drawing.Size(546, 24);
         this.mainMenu.Text = "menuStrip1";
         this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripMenuItem,
            this.openToolStripMenuItem,
            this.saveToolStripMenuItem,
            this.saveAsToolStripMenuItem,
            this.toolStripSeparator1,
            this.printToolStripMenuItem,
            this.printPreviewToolStripMenuItem,
            this.pageSetupToolStripMenuItem,
            this.toolStripSeparator2,
            this.exitToolStripMenuItem});
         this.fileToolStripMenuItem.Size = new System.Drawing.Size(38, 20);
         this.fileToolStripMenuItem.Text = "&File";
         // 
         this.newToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.newToolStripMenuItem.Text = "&New";
         this.newToolStripMenuItem.Click += new System.EventHandler(this.OnFileNew);
         // 
         this.openToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.openToolStripMenuItem.Text = "&Open...";
         this.openToolStripMenuItem.Click += new System.EventHandler(this.OnFileOpen);
         // 
         this.saveToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.saveToolStripMenuItem.Text = "&Save";
         this.saveToolStripMenuItem.Click += new System.EventHandler(this.OnFileSave);
         // 
         this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.saveAsToolStripMenuItem.Text = "Save &As...";
         this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.OnFileSaveAs);
         // 
         // dlgOpenFile
         // 
         this.dlgOpenFile.Filter = "Text Documents (*.txt)|*.txt|Wrox Documents (*.wroxtext)|*.wroxtext|All Files|*.*";
         // 
         // dlgSaveFile
         // 
         this.dlgSaveFile.FileName = "Untitled";
         this.dlgSaveFile.Filter = "Text Document (*.txt)|*.txt|Wrox Document (*.wroxtext)|*.wroxtext";
         this.dlgSaveFile.FilterIndex = 2;
         // 
         this.toolStripSeparator1.Size = new System.Drawing.Size(156, 6);
         // 
         this.printToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.printToolStripMenuItem.Text = "&Print...";
         this.printToolStripMenuItem.Click += new System.EventHandler(this.OnFilePrint);
         // 
         // printPreviewToolStripMenuItem
         // 
         this.printPreviewToolStripMenuItem.Name = "printPreviewToolStripMenuItem";
         this.printPreviewToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.printPreviewToolStripMenuItem.Text = "Print Pre&view...";
         this.printPreviewToolStripMenuItem.Click += new System.EventHandler(this.OnFilePrintPreview);
         // 
         // pageSetupToolStripMenuItem
         // 
         this.pageSetupToolStripMenuItem.Name = "pageSetupToolStripMenuItem";
         this.pageSetupToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.pageSetupToolStripMenuItem.Text = "Page Set&up...";
         this.pageSetupToolStripMenuItem.Click += new System.EventHandler(this.OnFilePageSetup);
         // 
         // toolStripSeparator2
         // 
         this.toolStripSeparator2.Name = "toolStripSeparator2";
         this.toolStripSeparator2.Size = new System.Drawing.Size(156, 6);
         // 
         // exitToolStripMenuItem
         // 
         this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
         this.exitToolStripMenuItem.Size = new System.Drawing.Size(159, 22);
         this.exitToolStripMenuItem.Text = "E&xit";
         // 
         // printDocument
         // 
         this.printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.OnPrintPage);
         this.printDocument.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.OnEndPrint);
         this.printDocument.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.OnBeginPrint);
         // 
         // dlgPageSetup
         // 
         this.dlgPageSetup.Document = this.printDocument;
         this.dlgPageSetup.ShowHelp = true;
         // 
         // dlgPrint
         // 
         this.dlgPrint.Document = this.printDocument;
         this.dlgPrint.UseEXDialog = true;
         // 
         // dlgPrintPreview
         // 
         this.dlgPrintPreview.AutoScrollMargin = new System.Drawing.Size(0, 0);
         this.dlgPrintPreview.AutoScrollMinSize = new System.Drawing.Size(0, 0);
         this.dlgPrintPreview.ClientSize = new System.Drawing.Size(400, 300);
         this.dlgPrintPreview.Document = this.printDocument;
         this.dlgPrintPreview.Enabled = true;
         this.dlgPrintPreview.Name = "dlgPrintPreview";
         this.dlgPrintPreview.Visible = false;
         // 
         // printPreviewControl
         // 
         this.printPreviewControl.Dock = System.Windows.Forms.DockStyle.Fill;
         this.printPreviewControl.Document = this.printDocument;
         this.printPreviewControl.Location = new System.Drawing.Point(0, 24);
         this.printPreviewControl.Name = "printPreviewControl";
         this.printPreviewControl.Size = new System.Drawing.Size(546, 203);
         this.printPreviewControl.TabIndex = 2;
         this.printPreviewControl.Visible = false;
         // 
         // SimpleEditorForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(546, 227);
         this.Controls.Add(this.printPreviewControl);
         this.Controls.Add(this.textBoxEdit);
         this.Controls.Add(this.mainMenu);
         this.MainMenuStrip = this.mainMenu;
         this.Name = "SimpleEditorForm";
         this.Text = "Simple Editor";
         this.mainMenu.ResumeLayout(false);
         this.mainMenu.PerformLayout();
         this.ResumeLayout(false);
         this.PerformLayout();
      }
      private System.Windows.Forms.TextBox textBoxEdit;
      private System.Windows.Forms.MenuStrip mainMenu;
      private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
      private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
      private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
      private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
      private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem;
      private System.Windows.Forms.OpenFileDialog dlgOpenFile;
      private System.Windows.Forms.SaveFileDialog dlgSaveFile;
      private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
      private System.Windows.Forms.ToolStripMenuItem printToolStripMenuItem;
      private System.Windows.Forms.ToolStripMenuItem printPreviewToolStripMenuItem;
      private System.Windows.Forms.ToolStripMenuItem pageSetupToolStripMenuItem;
      private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
      private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
      private System.Drawing.Printing.PrintDocument printDocument;
      private System.Windows.Forms.PageSetupDialog dlgPageSetup;
      private System.Windows.Forms.PrintDialog dlgPrint;
      private System.Windows.Forms.PrintPreviewDialog dlgPrintPreview;
      private System.Windows.Forms.PrintPreviewControl printPreviewControl;
      [STAThread]
      static void Main(string[] args)
      {
         
         Application.Run(new SimpleEditorForm());
      }
   }

TextBox cancel event

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class TextCancelEventKeyEvent : System.Windows.Forms.Form
{
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox txtInput;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label lblTrue;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label lblCheck;
    private System.Windows.Forms.Label lblResults;
  public TextCancelEventKeyEvent()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
     this.label1 = new System.Windows.Forms.Label();
     this.txtInput = new System.Windows.Forms.TextBox();
     this.label2 = new System.Windows.Forms.Label();
     this.lblTrue = new System.Windows.Forms.Label();
     this.label3 = new System.Windows.Forms.Label();
     this.lblCheck = new System.Windows.Forms.Label();
     this.lblResults = new System.Windows.Forms.Label();
     this.SuspendLayout();
     // 
     // label1
     // 
     this.label1.Font = new System.Drawing.Font("Tahoma", 14.25F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
     this.label1.Location = new System.Drawing.Point(48, 16);
     this.label1.Name = "label1";
     this.label1.Size = new System.Drawing.Size(176, 23);
     this.label1.TabIndex = 0;
     this.label1.Text = "ISBN Validation";
     // 
     // txtInput
     // 
     this.txtInput.Location = new System.Drawing.Point(72, 64);
     this.txtInput.Name = "txtInput";
     this.txtInput.TabIndex = 1;
     this.txtInput.Text = "";
     this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
     this.txtInput.Validating += new System.ruponentModel.CancelEventHandler(this.handleCancleEvent);
     // 
     // label2
     // 
     this.label2.Location = new System.Drawing.Point(24, 104);
     this.label2.Name = "label2";
     this.label2.Size = new System.Drawing.Size(80, 23);
     this.label2.TabIndex = 2;
     this.label2.Text = "True Number:";
     // 
     // lblTrue
     // 
     this.lblTrue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblTrue.Location = new System.Drawing.Point(112, 104);
     this.lblTrue.Name = "lblTrue";
     this.lblTrue.TabIndex = 3;
     // 
     // label3
     // 
     this.label3.Location = new System.Drawing.Point(32, 152);
     this.label3.Name = "label3";
     this.label3.Size = new System.Drawing.Size(72, 23);
     this.label3.TabIndex = 4;
     this.label3.Text = "Check Digit:";
     // 
     // lblCheck
     // 
     this.lblCheck.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblCheck.Location = new System.Drawing.Point(112, 152);
     this.lblCheck.Name = "lblCheck";
     this.lblCheck.TabIndex = 5;
     // 
     // lblResults
     // 
     this.lblResults.Location = new System.Drawing.Point(56, 192);
     this.lblResults.Name = "lblResults";
     this.lblResults.Size = new System.Drawing.Size(152, 24);
     this.lblResults.TabIndex = 8;
     // 
     // TextCancelEventKeyEvent
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(264, 293);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.lblResults,
                                                                  this.lblCheck,
                                                                  this.label3,
                                                                  this.lblTrue,
                                                                  this.label2,
                                                                  this.txtInput,
                                                                  this.label1});
     this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new TextCancelEventKeyEvent());
  }
  private void handleCancleEvent(object sender, System.ruponentModel.CancelEventArgs e)
  {
    TextBox tb = (TextBox)sender;
    string strInput = tb.Text;
        Console.WriteLine(strInput);
  }     
  private void txtInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
     char keyChar;
     keyChar = e.KeyChar;
     if(!Char.IsDigit(keyChar)      // 0 - 9
        &&
        keyChar != 8               // backspace
        &&
        keyChar != 13              // enter
        &&
        keyChar != "x"
        &&
        keyChar != 45              //  dash/minus
        ){
        //  Do not display the keystroke
        e.Handled = true;
     }
  }     
}

TextBox Keyevent hander

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class TextCancelEventKeyEvent : System.Windows.Forms.Form
{
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox txtInput;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label lblTrue;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label lblCheck;
    private System.Windows.Forms.Label lblResults;
  public TextCancelEventKeyEvent()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
     this.label1 = new System.Windows.Forms.Label();
     this.txtInput = new System.Windows.Forms.TextBox();
     this.label2 = new System.Windows.Forms.Label();
     this.lblTrue = new System.Windows.Forms.Label();
     this.label3 = new System.Windows.Forms.Label();
     this.lblCheck = new System.Windows.Forms.Label();
     this.lblResults = new System.Windows.Forms.Label();
     this.SuspendLayout();
     // 
     // label1
     // 
     this.label1.Font = new System.Drawing.Font("Tahoma", 14.25F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
     this.label1.Location = new System.Drawing.Point(48, 16);
     this.label1.Name = "label1";
     this.label1.Size = new System.Drawing.Size(176, 23);
     this.label1.TabIndex = 0;
     this.label1.Text = "ISBN Validation";
     // 
     // txtInput
     // 
     this.txtInput.Location = new System.Drawing.Point(72, 64);
     this.txtInput.Name = "txtInput";
     this.txtInput.TabIndex = 1;
     this.txtInput.Text = "";
     this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
     this.txtInput.Validating += new System.ruponentModel.CancelEventHandler(this.handleCancleEvent);
     // 
     // label2
     // 
     this.label2.Location = new System.Drawing.Point(24, 104);
     this.label2.Name = "label2";
     this.label2.Size = new System.Drawing.Size(80, 23);
     this.label2.TabIndex = 2;
     this.label2.Text = "True Number:";
     // 
     // lblTrue
     // 
     this.lblTrue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblTrue.Location = new System.Drawing.Point(112, 104);
     this.lblTrue.Name = "lblTrue";
     this.lblTrue.TabIndex = 3;
     // 
     // label3
     // 
     this.label3.Location = new System.Drawing.Point(32, 152);
     this.label3.Name = "label3";
     this.label3.Size = new System.Drawing.Size(72, 23);
     this.label3.TabIndex = 4;
     this.label3.Text = "Check Digit:";
     // 
     // lblCheck
     // 
     this.lblCheck.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
     this.lblCheck.Location = new System.Drawing.Point(112, 152);
     this.lblCheck.Name = "lblCheck";
     this.lblCheck.TabIndex = 5;
     // 
     // lblResults
     // 
     this.lblResults.Location = new System.Drawing.Point(56, 192);
     this.lblResults.Name = "lblResults";
     this.lblResults.Size = new System.Drawing.Size(152, 24);
     this.lblResults.TabIndex = 8;
     // 
     // TextCancelEventKeyEvent
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(264, 293);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.lblResults,
                                                                  this.lblCheck,
                                                                  this.label3,
                                                                  this.lblTrue,
                                                                  this.label2,
                                                                  this.txtInput,
                                                                  this.label1});
     this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new TextCancelEventKeyEvent());
  }
  private void handleCancleEvent(object sender, System.ruponentModel.CancelEventArgs e)
  {
    TextBox tb = (TextBox)sender;
    string strInput = tb.Text;
        Console.WriteLine(strInput);
  }     
  private void txtInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
     char keyChar;
     keyChar = e.KeyChar;
     if(!Char.IsDigit(keyChar)      // 0 - 9
        &&
        keyChar != 8               // backspace
        &&
        keyChar != 13              // enter
        &&
        keyChar != "x"
        &&
        keyChar != 45              //  dash/minus
        ){
        //  Do not display the keystroke
        e.Handled = true;
     }
  }     
}

TextBox key pressed event

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.buttonOK.Enabled = false;
        this.textBoxAddress.Tag = false;
        this.textBoxAge.Tag = false;
        this.textBoxName.Tag = false;
        this.textBoxName.Validating += new System.ruponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
        this.textBoxAddress.Validating += new
               System.ruponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
        this.textBoxAge.Validating += new
               System.ruponentModel.CancelEventHandler(this.textBoxEmpty_Validating);
        this.textBoxName.TextChanged += new System.EventHandler(this.textBox_TextChanged);
        this.textBoxAddress.TextChanged += new
                                        System.EventHandler(this.textBox_TextChanged);
        this.textBoxAge.TextChanged += new System.EventHandler(this.textBox_TextChanged);
    }
    private void buttonOK_Click(object sender, EventArgs e) {
        this.textBoxOutput.Text = "clicked";
    }
    private void buttonHelp_Click(object sender, EventArgs e) {
        this.textBoxOutput.Text = "text";
    }
    private void textBoxEmpty_Validating(object sender,
                                    System.ruponentModel.CancelEventArgs e) {
        TextBox tb = (TextBox)sender;
        if (tb.Text.Length == 0) {
            tb.BackColor = Color.Red;
            tb.Tag = false;
        } else {
            tb.BackColor = System.Drawing.SystemColors.Window;
            tb.Tag = true;
        }
        ValidateOK();
    }
    private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e) {
        if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            e.Handled = true; // Remove the character
    }
    private void textBox_TextChanged(object sender, System.EventArgs e) {
        TextBox tb = (TextBox)sender;
        if (tb.Text.Length == 0) {
            tb.Tag = false;
            tb.BackColor = Color.Red;
        } else {
            tb.Tag = true;
            tb.BackColor = SystemColors.Window;
        }
        ValidateOK();
    }
    private void ValidateOK() {
        this.buttonOK.Enabled = ((bool)(this.textBoxAddress.Tag) &&
                                (bool)(this.textBoxAge.Tag) &&
                                (bool)(this.textBoxName.Tag));
    }
    private void InitializeComponent() {
        this.labelName = new System.Windows.Forms.Label();
        this.labelAddress = new System.Windows.Forms.Label();
        this.textBoxName = new System.Windows.Forms.TextBox();
        this.textBoxAddress = new System.Windows.Forms.TextBox();
        this.labelOutput = new System.Windows.Forms.Label();
        this.buttonOK = new System.Windows.Forms.Button();
        this.buttonHelp = new System.Windows.Forms.Button();
        this.labelAge = new System.Windows.Forms.Label();
        this.textBoxAge = new System.Windows.Forms.TextBox();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.radioButtonMale = new System.Windows.Forms.RadioButton();
        this.radioButtonFemale = new System.Windows.Forms.RadioButton();
        this.checkBoxProgrammer = new System.Windows.Forms.CheckBox();
        this.textBoxOutput = new System.Windows.Forms.TextBox();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        this.labelName.AutoSize = true;
        this.labelName.Location = new System.Drawing.Point(13, 16);
        this.labelName.Name = "labelName";
        this.labelName.Size = new System.Drawing.Size(34, 14);
        this.labelName.TabIndex = 0;
        this.labelName.Text = "Name";
        this.labelAddress.AutoSize = true;
        this.labelAddress.Location = new System.Drawing.Point(13, 43);
        this.labelAddress.Name = "labelAddress";
        this.labelAddress.Size = new System.Drawing.Size(46, 14);
        this.labelAddress.TabIndex = 1;
        this.labelAddress.Text = "Address";
        this.textBoxName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBoxName.Location = new System.Drawing.Point(106, 13);
        this.textBoxName.Name = "textBoxName";
        this.textBoxName.Size = new System.Drawing.Size(225, 20);
        this.textBoxName.TabIndex = 2;
        this.textBoxAddress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBoxAddress.AutoSize = false;
        this.textBoxAddress.Location = new System.Drawing.Point(106, 40);
        this.textBoxAddress.Multiline = true;
        this.textBoxAddress.Name = "textBoxAddress";
        this.textBoxAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.textBoxAddress.Size = new System.Drawing.Size(225, 80);
        this.textBoxAddress.TabIndex = 3;
        this.labelOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.labelOutput.AutoSize = true;
        this.labelOutput.Location = new System.Drawing.Point(13, 233);
        this.labelOutput.Name = "labelOutput";
        this.labelOutput.Size = new System.Drawing.Size(38, 14);
        this.labelOutput.TabIndex = 6;
        this.labelOutput.Text = "Output";
        this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.buttonOK.Location = new System.Drawing.Point(338, 13);
        this.buttonOK.Name = "buttonOK";
        this.buttonOK.TabIndex = 8;
        this.buttonOK.Text = "OK";
        this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
        this.buttonHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.buttonHelp.CausesValidation = false;
        this.buttonHelp.Location = new System.Drawing.Point(338, 43);
        this.buttonHelp.Name = "buttonHelp";
        this.buttonHelp.TabIndex = 9;
        this.buttonHelp.Text = "Help";
        this.buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);
        this.labelAge.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.labelAge.AutoSize = true;
        this.labelAge.Location = new System.Drawing.Point(13, 212);
        this.labelAge.Name = "labelAge";
        this.labelAge.Size = new System.Drawing.Size(24, 14);
        this.labelAge.TabIndex = 10;
        this.labelAge.Text = "Age";
        this.textBoxAge.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBoxAge.Location = new System.Drawing.Point(106, 209);
        this.textBoxAge.MaxLength = 3;
        this.textBoxAge.Name = "textBoxAge";
        this.textBoxAge.TabIndex = 11;
        this.textBoxAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxAge_KeyPress);
        this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.groupBox1.Controls.Add(this.radioButtonMale);
        this.groupBox1.Controls.Add(this.radioButtonFemale);
        this.groupBox1.Location = new System.Drawing.Point(13, 152);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(318, 50);
        this.groupBox1.TabIndex = 12;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "Sex";
        this.radioButtonMale.AutoSize = true;
        this.radioButtonMale.Location = new System.Drawing.Point(182, 20);
        this.radioButtonMale.Name = "radioButtonMale";
        this.radioButtonMale.Size = new System.Drawing.Size(44, 17);
        this.radioButtonMale.TabIndex = 1;
        this.radioButtonMale.Text = "Male";
        this.radioButtonFemale.AutoSize = true;
        this.radioButtonFemale.Checked = true;
        this.radioButtonFemale.Location = new System.Drawing.Point(44, 20);
        this.radioButtonFemale.Name = "radioButtonFemale";
        this.radioButtonFemale.Size = new System.Drawing.Size(55, 17);
        this.radioButtonFemale.TabIndex = 0;
        this.radioButtonFemale.Text = "Female";
        this.checkBoxProgrammer.AutoSize = true;
        this.checkBoxProgrammer.Checked = true;
        this.checkBoxProgrammer.CheckState = System.Windows.Forms.CheckState.Checked;
        this.checkBoxProgrammer.Location = new System.Drawing.Point(13, 128);
        this.checkBoxProgrammer.Name = "checkBoxProgrammer";
        this.checkBoxProgrammer.Size = new System.Drawing.Size(78, 17);
        this.checkBoxProgrammer.TabIndex = 13;
        this.checkBoxProgrammer.Text = "Programmer";
        this.textBoxOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBoxOutput.AutoSize = false;
        this.textBoxOutput.Location = new System.Drawing.Point(13, 254);
        this.textBoxOutput.Multiline = true;
        this.textBoxOutput.Name = "textBoxOutput";
        this.textBoxOutput.ReadOnly = true;
        this.textBoxOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.textBoxOutput.Size = new System.Drawing.Size(318, 116);
        this.textBoxOutput.TabIndex = 14;
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(422, 382);
        this.Controls.Add(this.textBoxOutput);
        this.Controls.Add(this.checkBoxProgrammer);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.textBoxAge);
        this.Controls.Add(this.labelAge);
        this.Controls.Add(this.buttonHelp);
        this.Controls.Add(this.buttonOK);
        this.Controls.Add(this.labelOutput);
        this.Controls.Add(this.textBoxAddress);
        this.Controls.Add(this.textBoxName);
        this.Controls.Add(this.labelAddress);
        this.Controls.Add(this.labelName);
        this.MinimumSize = new System.Drawing.Size(430, 328);
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();
    }

    private System.Windows.Forms.Label labelName;
    private System.Windows.Forms.Label labelAddress;
    private System.Windows.Forms.TextBox textBoxName;
    private System.Windows.Forms.TextBox textBoxAddress;
    private System.Windows.Forms.Label labelOutput;
    private System.Windows.Forms.Button buttonOK;
    private System.Windows.Forms.Button buttonHelp;
    private System.Windows.Forms.Label labelAge;
    private System.Windows.Forms.TextBox textBoxAge;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.RadioButton radioButtonFemale;
    private System.Windows.Forms.RadioButton radioButtonMale;
    private System.Windows.Forms.CheckBox checkBoxProgrammer;
    private System.Windows.Forms.TextBox textBoxOutput;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

TextBox Modified and TextChanged

using System;
using System.Drawing;
using System.Windows.Forms;
public class TextBoxTextChanged : Form
{
  TextBox txt;
  string strOriginal;
  public TextBoxTextChanged()
  {
    Size = new Size(300, 375);
               
    txt = new TextBox();
    txt.Parent = this;
    txt.Text = "Enter text here.";
    txt.Size = new Size(ClientSize.Width - 20, ClientSize.Height - 100);
    txt.Location = new Point(10,10);
    txt.TextChanged += new System.EventHandler(txt_TextChanged);
    txt.Multiline = true;
    txt.BorderStyle = BorderStyle.Fixed3D;
    txt.ScrollBars = ScrollBars.Vertical;
    txt.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    strOriginal = txt.Text;
  }
  static void Main() 
  {
    Application.Run(new TextBoxTextChanged());
  }
  private void txt_TextChanged(object sender, EventArgs e)
  {
    if (strOriginal == txt.Text)
      txt.Modified = false;
    else
      txt.Modified = true;
    if (txt.Modified)
      MessageBox.Show("changed");
    else
      MessageBox.Show("no change");
  }
}

TextBox: Multiline and BorderStyle

using System;
using System.Drawing;
using System.Windows.Forms;
public class TextBoxTextChanged : Form
{
  TextBox txt;
  string strOriginal;
  public TextBoxTextChanged()
  {
    Size = new Size(300, 375);
               
    txt = new TextBox();
    txt.Parent = this;
    txt.Text = "Enter text here.";
    txt.Size = new Size(ClientSize.Width - 20, ClientSize.Height - 100);
    txt.Location = new Point(10,10);
    txt.Multiline = true;
    txt.BorderStyle = BorderStyle.Fixed3D;
    txt.ScrollBars = ScrollBars.Vertical;
    txt.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    strOriginal = txt.Text;
  }
  static void Main() 
  {
    Application.Run(new TextBoxTextChanged());
  }
}

TextBox selection

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void showButton_Click(object sender, EventArgs e)
    {
        if (inputBox.SelectionLength == 0)
        {
            MessageBox.Show("You need to select something inside the text box first");
        }
        else
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append(String.Format("The input box contains {0}\n", inputBox.Text));
            builder.Append(String.Format("You have selected {0} characters, starting at {1}\n", inputBox.SelectionLength, inputBox.SelectionStart));
            builder.Append(String.Format("The selection is {0}", inputBox.SelectedText));
            MessageBox.Show(builder.ToString());
        }
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.inputBox = new System.Windows.Forms.TextBox();
        this.showButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // inputBox
        // 
        this.inputBox.Location = new System.Drawing.Point(16, 15);
        this.inputBox.Name = "inputBox";
        this.inputBox.Size = new System.Drawing.Size(421, 22);
        this.inputBox.TabIndex = 0;
        // 
        // showButton
        // 
        this.showButton.Location = new System.Drawing.Point(311, 46);
        this.showButton.Name = "showButton";
        this.showButton.Size = new System.Drawing.Size(126, 27);
        this.showButton.TabIndex = 1;
        this.showButton.Text = "Show selection";
        this.showButton.Click += new System.EventHandler(this.showButton_Click);
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
        this.ClientSize = new System.Drawing.Size(449, 87);
        this.Controls.Add(this.showButton);
        this.Controls.Add(this.inputBox);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.TextBox inputBox;
    private System.Windows.Forms.Button showButton;
}
public class TextBoxSelection
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

TextBox validation

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void numberBox_Validating(object sender, CancelEventArgs e)
    {
        try
        {
            int numberEntered = int.Parse(numberBox.Text);
            if (numberEntered < 1 || numberEntered > 10)
            {
                e.Cancel = true;
                MessageBox.Show("You have to enter a number between 1 and 10");
            }
        }
        catch (FormatException)
        {
            e.Cancel = true;
            MessageBox.Show("You need to enter an integer");
        }
    }
    private void numberBox_Validated(object sender, EventArgs e)
    {
        MessageBox.Show("Well done, you managed to enter a valid number");
    }
    private void okButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.numberBox = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.okButton = new System.Windows.Forms.Button();
        this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
        this.SuspendLayout();
        // 
        // numberBox
        // 
        this.numberBox.Location = new System.Drawing.Point(253, 15);
        this.numberBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.numberBox.Name = "numberBox";
        this.numberBox.Size = new System.Drawing.Size(57, 22);
        this.numberBox.TabIndex = 0;
        this.numberBox.Validated += new System.EventHandler(this.numberBox_Validated);
        this.numberBox.Validating += new System.ruponentModel.CancelEventHandler(this.numberBox_Validating);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(16, 18);
        this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(201, 16);
        this.label1.TabIndex = 1;
        this.label1.Text = "Enter a number between 1 and 10";
        // 
        // okButton
        // 
        this.okButton.Location = new System.Drawing.Point(335, 11);
        this.okButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.okButton.Name = "okButton";
        this.okButton.Size = new System.Drawing.Size(100, 28);
        this.okButton.TabIndex = 2;
        this.okButton.Text = "OK";
        this.okButton.Click += new System.EventHandler(this.okButton_Click);
        // 
        // maskedTextBox1
        // 
        this.maskedTextBox1.Location = new System.Drawing.Point(0, 0);
        this.maskedTextBox1.Name = "maskedTextBox1";
        this.maskedTextBox1.Size = new System.Drawing.Size(100, 23);
        this.maskedTextBox1.TabIndex = 3;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(453, 246);
        this.Controls.Add(this.maskedTextBox1);
        this.Controls.Add(this.okButton);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.numberBox);
        this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.TextBox numberBox;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button okButton;
    private System.Windows.Forms.MaskedTextBox maskedTextBox1;
}
public class TextBoxValidation
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

Use Regular Expression to Check the input for a TextBox

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
public class TextBoxValidationRegex : Form
{
    public TextBoxValidationRegex()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.Button1 = new System.Windows.Forms.Button();
        this.errProvider = new System.Windows.Forms.ErrorProvider(this.ruponents);
        this.Label3 = new System.Windows.Forms.Label();
        this.txtEmail = new System.Windows.Forms.TextBox();
        ((System.ruponentModel.ISupportInitialize)(this.errProvider)).BeginInit();
        this.SuspendLayout();
        this.Button1.Location = new System.Drawing.Point(212, 80);
        this.Button1.Name = "Button1";
        this.Button1.Size = new System.Drawing.Size(76, 24);
        this.Button1.TabIndex = 12;
        this.Button1.Text = "OK";
        this.Button1.Click += new System.EventHandler(this.Button1_Click);
        this.errProvider.ContainerControl = this;
        this.errProvider.DataMember = "";
        this.Label3.Location = new System.Drawing.Point(24, 24);
        this.Label3.Size = new System.Drawing.Size(40, 16);
        this.Label3.Text = "Email:";
        this.txtEmail.Location = new System.Drawing.Point(68, 20);
        this.txtEmail.Size = new System.Drawing.Size(220, 21);
        this.txtEmail.Leave += new System.EventHandler(this.txtEmail_Leave);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(328, 126);
        this.Controls.Add(this.Label3);
        this.Controls.Add(this.txtEmail);
        this.Controls.Add(this.Button1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        ((System.ruponentModel.ISupportInitialize)(this.errProvider)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private void Button1_Click(object sender, EventArgs e)
    {
        string errorText = "";
        foreach (Control ctrl in this.Controls)
        {
            if (errProvider.GetError(ctrl) != "")
            {
                errorText += "   * " + errProvider.GetError(ctrl) + "\n";
                
            }
            MessageBox.Show("Errors:" +errorText, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
    private void txtEmail_Leave(object sender, EventArgs e)
    {
        Regex regex;
        regex = new Regex(@"^[\w-]+@([\w-]+\.)+[\w-]+$");
        Control ctrl = (Control)sender;
        if (regex.IsMatch(ctrl.Text) || ctrl.Text == "")
        {
            errProvider.SetError(ctrl, "");
        }
        else
        {
            errProvider.SetError(ctrl, "This is not a valid email address.");
        }
    }
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new TextBoxValidationRegex());
    }
    private System.Windows.Forms.Button Button1;
    private System.Windows.Forms.ErrorProvider errProvider;
    private System.Windows.Forms.Label Label3;
    private System.Windows.Forms.TextBox txtEmail;
    private System.ruponentModel.IContainer components = null;
}