Csharp/C Sharp by API/System.Windows.Forms/TextBox
Содержание
- 1 TextBox.AcceptsTab
- 2 TextBox.Anchor
- 3 TextBox.BorderStyle
- 4 TextBox.CanFocus
- 5 TextBox.Clear()
- 6 TextBox.Click
- 7 TextBox.ContainsFocus
- 8 TextBox.ContextMenu
- 9 TextBox.DataBindings
- 10 TextBox.DoDragDrop
- 11 TextBox.DragDrop
- 12 TextBox.DragEnter
- 13 TextBox.Focus()
- 14 TextBox.Focused
- 15 TextBox.GotFocus
- 16 TextBox.KeyPress
- 17 TextBox.Lines
- 18 TextBox.LostFocus
- 19 TextBox.MouseDown
- 20 TextBox.Multiline
- 21 TextBox.PasswordChar
- 22 TextBox.ScrollBars
- 23 TextBox.SelectAll()
- 24 TextBox.SelectedText
- 25 TextBox.SelectionLength
- 26 TextBox.TextChanged
- 27 TextBox.Validated
- 28 TextBox.Validating
TextBox.AcceptsTab
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace TextBox {
/// <summary> /// Summary description for TextBox. /// </summary> public class TextBox : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox SINGLE_LINE; private System.Windows.Forms.TextBox PASSWORD_LINE; private System.Windows.Forms.TextBox MULTI_LINE; private System.Windows.Forms.RichTextBox RICH_EDIT; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public TextBox() { // // 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.SINGLE_LINE = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.PASSWORD_LINE = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.MULTI_LINE = new System.Windows.Forms.TextBox(); this.RICH_EDIT = new System.Windows.Forms.RichTextBox(); this.label4 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // SINGLE_LINE // this.SINGLE_LINE.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper; this.SINGLE_LINE.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(64))); this.SINGLE_LINE.Location = new System.Drawing.Point(32, 40); this.SINGLE_LINE.Name = "SINGLE_LINE"; this.SINGLE_LINE.Size = new System.Drawing.Size(408, 20); this.SINGLE_LINE.TabIndex = 0; this.SINGLE_LINE.Text = "THIS IS A SINGLELINE EDIT CONTROL"; // // label1 // this.label1.Location = new System.Drawing.Point(32, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(168, 16); this.label1.TabIndex = 1; this.label1.Text = "Single Line TextBox"; // // PASSWORD_LINE // this.PASSWORD_LINE.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.PASSWORD_LINE.Location = new System.Drawing.Point(32, 104); this.PASSWORD_LINE.Name = "PASSWORD_LINE"; this.PASSWORD_LINE.PasswordChar = "^"; this.PASSWORD_LINE.Size = new System.Drawing.Size(104, 22); this.PASSWORD_LINE.TabIndex = 2; this.PASSWORD_LINE.Text = ""; // // label2 // this.label2.Location = new System.Drawing.Point(32, 80); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(160, 16); this.label2.TabIndex = 3; this.label2.Text = "Password TextBox"; // // label3 // this.label3.Location = new System.Drawing.Point(32, 136); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(120, 16); this.label3.TabIndex = 4; this.label3.Text = "MultiLine TextBox"; // // MULTI_LINE // this.MULTI_LINE.AcceptsReturn = true; this.MULTI_LINE.AcceptsTab = true; this.MULTI_LINE.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.MULTI_LINE.ForeColor = System.Drawing.SystemColors.HotTrack; this.MULTI_LINE.Location = new System.Drawing.Point(32, 160); this.MULTI_LINE.MaxLength = 10000; this.MULTI_LINE.Multiline = true; this.MULTI_LINE.Name = "MULTI_LINE"; this.MULTI_LINE.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.MULTI_LINE.Size = new System.Drawing.Size(408, 104); this.MULTI_LINE.TabIndex = 5; this.MULTI_LINE.Text = ""; // // RICH_EDIT // this.RICH_EDIT.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.RICH_EDIT.ForeColor = System.Drawing.Color.Red; this.RICH_EDIT.Location = new System.Drawing.Point(32, 296); this.RICH_EDIT.Name = "RICH_EDIT"; this.RICH_EDIT.ShowSelectionMargin = true; this.RICH_EDIT.Size = new System.Drawing.Size(408, 120); this.RICH_EDIT.TabIndex = 6; this.RICH_EDIT.Text = "richTextBox1"; this.RICH_EDIT.ZoomFactor = 4.999695F; // // label4 // this.label4.Location = new System.Drawing.Point(32, 272); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(152, 16); this.label4.TabIndex = 7; this.label4.Text = "RichEdit Text Box"; // // TextBox // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 453); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label4, this.RICH_EDIT, this.MULTI_LINE, this.label3, this.label2, this.PASSWORD_LINE, this.label1, this.SINGLE_LINE}); this.Name = "TextBox"; this.Text = "TextBox Controls"; this.Load += new System.EventHandler(this.TextBox_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new TextBox()); } private void TextBox_Load(object sender, System.EventArgs e) { RICH_EDIT.LoadFile("c:\\temp\\RTFDOC.RTF"); } }
}
</source>
TextBox.Anchor
<source lang="csharp"> 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()); }
}
</source>
TextBox.BorderStyle
<source lang="csharp"> 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"); }
}
</source>
TextBox.CanFocus
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.Text = "This button does nothing"; 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.MaximizeBox = false; this.Name = "FocusForm"; this.Text = "FocusForm"; this.ResumeLayout(false); } static void Main() { Application.Run(new FocusForm()); } protected void txtFocusForm_LostFocus(object sender, EventArgs e) { // MessageBox.Show("Goodbye!"); } protected void txtFocusForm_GotFocus(object sender, EventArgs e) { // MessageBox.Show("Hello!"); } private void btFocusForm_Click(object sender, System.EventArgs e) { bool canFocus = txtFocusForm.CanFocus; bool containsFocus = this.ContainsFocus; bool focused = txtFocusForm.Focused; MessageBox.Show("Textbox can focus: " + canFocus + "\nForm children contain focus: " + containsFocus + "\nTextbox has focus: " + focused); txtFocusForm.Focus(); } }
</source>
TextBox.Clear()
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; public class SimpleEditorForm : Form {
private string filename = "Untitled"; public SimpleEditorForm(string filename) { InitializeComponent(); if (filename != null) { this.filename = filename; OpenFile(); } } protected 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"; textBoxEdit.Clear(); } private void OnFileOpen(object sender, EventArgs e) { if (dlgOpenFile.ShowDialog() == DialogResult.OK) { filename = dlgOpenFile.FileName; OpenFile(); } } private void OnFileSave(object sender, EventArgs e) { } private void OnFileSaveAs(object sender, EventArgs e) { } private void InitializeComponent() { this.textBoxEdit = new System.Windows.Forms.TextBox(); this.mainMenu = new System.Windows.Forms.MenuStrip(); this.miFile = new System.Windows.Forms.ToolStripMenuItem(); this.miFileNew = new System.Windows.Forms.ToolStripMenuItem(); this.miFileOpen = new System.Windows.Forms.ToolStripMenuItem(); this.miFileSave = new System.Windows.Forms.ToolStripMenuItem(); this.miFileSaveAs = new System.Windows.Forms.ToolStripMenuItem(); this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog(); this.mainMenu.SuspendLayout(); this.SuspendLayout(); // // textBoxEdit // 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(562, 219); this.textBoxEdit.TabIndex = 0; // // mainMenu // this.mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.miFile}); this.mainMenu.Location = new System.Drawing.Point(0, 0); this.mainMenu.Name = "mainMenu"; this.mainMenu.Size = new System.Drawing.Size(562, 24); this.mainMenu.TabIndex = 1; this.mainMenu.Text = "menuStrip1"; // // miFile // this.miFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.miFileNew, this.miFileOpen, this.miFileSave, this.miFileSaveAs}); this.miFile.Name = "miFile"; this.miFile.Text = "&File"; // // miFileNew // this.miFileNew.Name = "miFileNew"; this.miFileNew.Text = "&New"; this.miFileNew.Click += new System.EventHandler(this.OnFileNew); // // miFileOpen // this.miFileOpen.Name = "miFileOpen"; this.miFileOpen.Text = "&Open"; this.miFileOpen.Click += new System.EventHandler(this.OnFileOpen); // // miFileSave // this.miFileSave.Name = "miFileSave"; this.miFileSave.Text = "&Save"; this.miFileSave.Click += new System.EventHandler(this.OnFileSave); // // miFileSaveAs // this.miFileSaveAs.Name = "miFileSaveAs"; this.miFileSaveAs.Text = "Save &As"; this.miFileSaveAs.Click += new System.EventHandler(this.OnFileSaveAs); // // dlgOpenFile // this.dlgOpenFile.Filter = "Text Documents (*.txt)|*.txt|Wrox Documents (*.wroxtext)|*.wroxtext|All Files|*.*" + ""; this.dlgOpenFile.FilterIndex = 2; // // SimpleEditorForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(562, 243); 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.ResumeLayout(false); this.PerformLayout(); }
private System.Windows.Forms.TextBox textBoxEdit; private System.Windows.Forms.MenuStrip mainMenu; private System.Windows.Forms.ToolStripMenuItem miFile; private System.Windows.Forms.ToolStripMenuItem miFileNew; private System.Windows.Forms.ToolStripMenuItem miFileOpen; private System.Windows.Forms.ToolStripMenuItem miFileSave; private System.Windows.Forms.ToolStripMenuItem miFileSaveAs; private System.Windows.Forms.OpenFileDialog dlgOpenFile; [STAThread] static void Main(string[] args) { string filename = null; if (args.Length != 0) filename = args[0]; Application.EnableVisualStyles(); Application.Run(new SimpleEditorForm(filename)); }
}
</source>
TextBox.Click
<source lang="csharp">
using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {
Label Label1; TextBox TextBox1; Button Button1; public Form1() { InitializeComponent(); } private void ctrlClick(System.Object sender, EventArgs e) { Control ctrl = (Control)sender; MessageBox.Show("You clicked: " + ctrl.Name); } private void InitializeComponent() { this.Label1 = new System.Windows.Forms.Label(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.Button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // Label1 // this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Label1.Location = new System.Drawing.Point(14, 97); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(112, 24); this.Label1.TabIndex = 8; this.Label1.Text = "Label1"; this.Label1.Click += new System.EventHandler(this.ctrlClick); // // TextBox1 // this.TextBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.TextBox1.Location = new System.Drawing.Point(12, 61); this.TextBox1.Name = "TextBox1"; this.TextBox1.Size = new System.Drawing.Size(156, 21); this.TextBox1.TabIndex = 7; this.TextBox1.Text = "TextBox1"; this.TextBox1.Click += new System.EventHandler(this.ctrlClick); // // Button1 // this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.Button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Button1.Location = new System.Drawing.Point(12, 21); this.Button1.Name = "Button1"; this.Button1.Size = new System.Drawing.Size(96, 28); this.Button1.TabIndex = 6; this.Button1.Text = "Button1"; this.Button1.Click += new System.EventHandler(this.ctrlClick); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(367, 281); this.Controls.Add(this.Label1); this.Controls.Add(this.TextBox1); this.Controls.Add(this.Button1); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "Form1"; this.Text = "Control Medley"; this.ResumeLayout(false); this.PerformLayout(); }
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}
</source>
TextBox.ContainsFocus
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.Text = "This button does nothing"; 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.MaximizeBox = false; this.Name = "FocusForm"; this.Text = "FocusForm"; this.ResumeLayout(false); } static void Main() { Application.Run(new FocusForm()); } protected void txtFocusForm_LostFocus(object sender, EventArgs e) { // MessageBox.Show("Goodbye!"); } protected void txtFocusForm_GotFocus(object sender, EventArgs e) { // MessageBox.Show("Hello!"); } private void btFocusForm_Click(object sender, System.EventArgs e) { bool canFocus = txtFocusForm.CanFocus; bool containsFocus = this.ContainsFocus; bool focused = txtFocusForm.Focused; MessageBox.Show("Textbox can focus: " + canFocus + "\nForm children contain focus: " + containsFocus + "\nTextbox has focus: " + focused); txtFocusForm.Focus(); } }
</source>
TextBox.ContextMenu
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; public class TextBoxContextMenuDemo : Form {
public TextBoxContextMenuDemo() { InitializeComponent(); ContextMenu mnuContext = new ContextMenu(); foreach (MenuItem mnuItem in mnuFile.MenuItems) { mnuContext.MenuItems.Add(mnuItem.CloneMenu()); } TextBox1.ContextMenu = mnuContext; } private void TextBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { TextBox1.ContextMenu.Show(TextBox1, new Point(e.X, e.Y)); } } private void mnuOpen_Click(object sender, EventArgs e) { MessageBox.Show("This is the event handler for Open.","TextBoxContextMenuDemo"); } private void mnuSave_Click(object sender, EventArgs e) { MessageBox.Show("This is the event handler for Save.","TextBoxContextMenuDemo"); } private void mnuExit_Click(object sender, EventArgs e) { MessageBox.Show("This is the event handler for Exit.","TextBoxContextMenuDemo"); } [STAThread] public static void Main(string[] args) { Application.Run(new TextBoxContextMenuDemo()); } private System.Windows.Forms.MainMenu MainMenu1; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuOpen; private System.Windows.Forms.MenuItem mnuSave; private System.Windows.Forms.MenuItem mnuExit; private System.Windows.Forms.MenuItem MenuItem5; private System.Windows.Forms.MenuItem MenuItem6; private System.Windows.Forms.MenuItem MenuItem7; private System.Windows.Forms.MenuItem MenuItem8; private System.Windows.Forms.TextBox TextBox1; private System.ruponentModel.IContainer components = null; private void InitializeComponent() { this.ruponents = new System.ruponentModel.Container(); this.MainMenu1 = new System.Windows.Forms.MainMenu(this.ruponents); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuOpen = new System.Windows.Forms.MenuItem(); this.mnuSave = new System.Windows.Forms.MenuItem(); this.mnuExit = new System.Windows.Forms.MenuItem(); this.MenuItem5 = new System.Windows.Forms.MenuItem(); this.MenuItem6 = new System.Windows.Forms.MenuItem(); this.MenuItem7 = new System.Windows.Forms.MenuItem(); this.MenuItem8 = new System.Windows.Forms.MenuItem(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // MainMenu1 // this.MainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.MenuItem5}); // // mnuFile // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuOpen, this.mnuSave, this.mnuExit}); this.mnuFile.Text = "File"; // // mnuOpen // this.mnuOpen.Index = 0; this.mnuOpen.Text = "Open"; this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click); // // mnuSave // this.mnuSave.Index = 1; this.mnuSave.Text = "Save"; this.mnuSave.Click += new System.EventHandler(this.mnuSave_Click); // // mnuExit // this.mnuExit.Index = 2; this.mnuExit.Text = "Exit"; this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click); // // MenuItem5 // this.MenuItem5.Index = 1; this.MenuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.MenuItem6, this.MenuItem7, this.MenuItem8}); this.MenuItem5.Text = "Edit"; // // MenuItem6 // this.MenuItem6.Index = 0; this.MenuItem6.Text = "Cut"; // // MenuItem7 // this.MenuItem7.Index = 1; this.MenuItem7.Text = "Copy"; // // MenuItem8 // this.MenuItem8.Index = 2; this.MenuItem8.Text = "Paste"; // // TextBox1 // this.TextBox1.Location = new System.Drawing.Point(44, 56); this.TextBox1.Multiline = true; this.TextBox1.Name = "TextBox1"; this.TextBox1.Size = new System.Drawing.Size(180, 88); this.TextBox1.TabIndex = 1; this.TextBox1.Text = "Right click here to view the cloned context menu."; this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox1_MouseDown); // // TextBoxContextMenuDemo // 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.TextBox1); this.Menu = this.MainMenu1; this.ResumeLayout(false); this.PerformLayout(); }
}
</source>
TextBox.DataBindings
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient;
public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button buttonBack; private System.Windows.Forms.Button buttonNext; private System.Data.DataSet dataSet1; private System.ruponentModel.Container components = null; private BindingManagerBase bMgr; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.buttonBack = new System.Windows.Forms.Button(); this.buttonNext = new System.Windows.Forms.Button(); this.dataSet1 = new System.Data.DataSet(); ((System.ruponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); this.SuspendLayout(); this.textBox1.Location = new System.Drawing.Point(8, 8); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(160, 20); this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1"; this.textBox2.Location = new System.Drawing.Point(8, 40); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(160, 20); this.textBox2.TabIndex = 1; this.textBox2.Text = "textBox2"; this.buttonBack.Location = new System.Drawing.Point(24, 80); this.buttonBack.Name = "buttonBack"; this.buttonBack.Size = new System.Drawing.Size(56, 23); this.buttonBack.TabIndex = 2; this.buttonBack.Text = "<< Back"; this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click); this.buttonNext.Location = new System.Drawing.Point(96, 80); this.buttonNext.Name = "buttonNext"; this.buttonNext.Size = new System.Drawing.Size(56, 23); this.buttonNext.TabIndex = 3; this.buttonNext.Text = "Next>>"; this.buttonNext.Click += new System.EventHandler(this.buttonNext_Click); this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US"); this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(176, 108); this.Controls.Add(this.buttonNext); this.Controls.Add(this.buttonBack); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ruponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); this.ResumeLayout(false); } static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI"; string sql = @"select * from employee "; SqlConnection conn = new SqlConnection(connString); SqlDataAdapter da = new SqlDataAdapter(sql, conn); da.Fill(dataSet1, "employee"); textBox1.DataBindings.Add("text", dataSet1, "employee.firstname"); textBox2.DataBindings.Add("text", dataSet1, "employee.lastname"); bMgr = this.BindingContext[dataSet1, "employee"]; } private void buttonNext_Click(object sender, System.EventArgs e) { bMgr.Position += 1; } private void buttonBack_Click(object sender, System.EventArgs e) { bMgr.Position -= 1; } } </source>
TextBox.DoDragDrop
<source lang="csharp">
using System; using System.Windows.Forms; public class TextBoxDragDropDemo : Form {
public TextBoxDragDropDemo() { InitializeComponent(); } private void TextBox_MouseDown(object sender, MouseEventArgs e) { TextBox txt = (TextBox)sender; txt.SelectAll(); txt.DoDragDrop(txt.Text, DragDropEffects.Copy); } private void TextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { TextBox txt = (TextBox)sender; txt.Text = (string)e.Data.GetData(DataFormats.Text); } [STAThread] public static void Main(string[] args) { Application.Run(new TextBoxDragDropDemo()); } private System.Windows.Forms.TextBox TextBox2; private System.Windows.Forms.TextBox TextBox1; private void InitializeComponent() { this.TextBox2 = new System.Windows.Forms.TextBox(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.TextBox2.AllowDrop = true; this.TextBox2.Location = new System.Drawing.Point(28, 129); this.TextBox2.Multiline = true; this.TextBox2.Size = new System.Drawing.Size(196, 77); this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); this.TextBox1.AllowDrop = true; this.TextBox1.Location = new System.Drawing.Point(28, 36); this.TextBox1.Multiline = true; this.TextBox1.Size = new System.Drawing.Size(196, 77); this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); 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.TextBox2); this.Controls.Add(this.TextBox1); this.ResumeLayout(false); this.PerformLayout(); }
}
</source>
TextBox.DragDrop
<source lang="csharp">
using System; using System.Windows.Forms; public class TextBoxDragDropDemo : Form {
public TextBoxDragDropDemo() { InitializeComponent(); } private void TextBox_MouseDown(object sender, MouseEventArgs e) { TextBox txt = (TextBox)sender; txt.SelectAll(); txt.DoDragDrop(txt.Text, DragDropEffects.Copy); } private void TextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { TextBox txt = (TextBox)sender; txt.Text = (string)e.Data.GetData(DataFormats.Text); } [STAThread] public static void Main(string[] args) { Application.Run(new TextBoxDragDropDemo()); } private System.Windows.Forms.TextBox TextBox2; private System.Windows.Forms.TextBox TextBox1; private void InitializeComponent() { this.TextBox2 = new System.Windows.Forms.TextBox(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.TextBox2.AllowDrop = true; this.TextBox2.Location = new System.Drawing.Point(28, 129); this.TextBox2.Multiline = true; this.TextBox2.Size = new System.Drawing.Size(196, 77); this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); this.TextBox1.AllowDrop = true; this.TextBox1.Location = new System.Drawing.Point(28, 36); this.TextBox1.Multiline = true; this.TextBox1.Size = new System.Drawing.Size(196, 77); this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); 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.TextBox2); this.Controls.Add(this.TextBox1); this.ResumeLayout(false); this.PerformLayout(); }
}
</source>
TextBox.DragEnter
<source lang="csharp">
using System; using System.Windows.Forms; public class TextBoxDragDropDemo : Form {
public TextBoxDragDropDemo() { InitializeComponent(); } private void TextBox_MouseDown(object sender, MouseEventArgs e) { TextBox txt = (TextBox)sender; txt.SelectAll(); txt.DoDragDrop(txt.Text, DragDropEffects.Copy); } private void TextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { TextBox txt = (TextBox)sender; txt.Text = (string)e.Data.GetData(DataFormats.Text); } [STAThread] public static void Main(string[] args) { Application.Run(new TextBoxDragDropDemo()); } private System.Windows.Forms.TextBox TextBox2; private System.Windows.Forms.TextBox TextBox1; private void InitializeComponent() { this.TextBox2 = new System.Windows.Forms.TextBox(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.TextBox2.AllowDrop = true; this.TextBox2.Location = new System.Drawing.Point(28, 129); this.TextBox2.Multiline = true; this.TextBox2.Size = new System.Drawing.Size(196, 77); this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); this.TextBox1.AllowDrop = true; this.TextBox1.Location = new System.Drawing.Point(28, 36); this.TextBox1.Multiline = true; this.TextBox1.Size = new System.Drawing.Size(196, 77); this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); 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.TextBox2); this.Controls.Add(this.TextBox1); this.ResumeLayout(false); this.PerformLayout(); }
}
</source>
TextBox.Focus()
<source lang="csharp">
using System; using System.Drawing; using System.Windows.Forms; public class EnterPrice : Form {
private Button enter = new Button(); private Label answer = new Label(); private TextBox text = new TextBox( ); public EnterPrice( ) { enter.Text = "Enter Price"; text.Text = ""; answer.Text = ""; Size = new Size(300,200); answer.Size = new Size(200,50); enter.Location = new Point(30 + enter.Width, 30); text.Location = new Point (40 + enter.Width + enter.Width, 30); answer.Location = new Point(20, 60); AcceptButton = enter; Controls.Add(text); Controls.Add(answer); Controls.Add(enter); enter.Click += new EventHandler(Enter_Click); } protected void Enter_Click(Object sender, EventArgs e) { try{ Console.WriteLine(Double.Parse(text.Text)); }catch(Exception){ } text.Text = ""; text.Focus(); } static void Main() { Application.Run(new EnterPrice()); }
}
</source>
TextBox.Focused
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.Text = "This button does nothing"; 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.MaximizeBox = false; this.Name = "FocusForm"; this.Text = "FocusForm"; this.ResumeLayout(false); } static void Main() { Application.Run(new FocusForm()); } protected void txtFocusForm_LostFocus(object sender, EventArgs e) { // MessageBox.Show("Goodbye!"); } protected void txtFocusForm_GotFocus(object sender, EventArgs e) { // MessageBox.Show("Hello!"); } private void btFocusForm_Click(object sender, System.EventArgs e) { bool canFocus = txtFocusForm.CanFocus; bool containsFocus = this.ContainsFocus; bool focused = txtFocusForm.Focused; MessageBox.Show("Textbox can focus: " + canFocus + "\nForm children contain focus: " + containsFocus + "\nTextbox has focus: " + focused); txtFocusForm.Focus(); } }
</source>
TextBox.GotFocus
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.Text = "This button does nothing"; 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.MaximizeBox = false; this.Name = "FocusForm"; this.Text = "FocusForm"; this.ResumeLayout(false); } static void Main() { Application.Run(new FocusForm()); } protected void txtFocusForm_LostFocus(object sender, EventArgs e) { // MessageBox.Show("Goodbye!"); } protected void txtFocusForm_GotFocus(object sender, EventArgs e) { // MessageBox.Show("Hello!"); } private void btFocusForm_Click(object sender, System.EventArgs e) { bool canFocus = txtFocusForm.CanFocus; bool containsFocus = this.ContainsFocus; bool focused = txtFocusForm.Focused; MessageBox.Show("Textbox can focus: " + canFocus + "\nForm children contain focus: " + containsFocus + "\nTextbox has focus: " + focused); txtFocusForm.Focus(); } }
</source>
TextBox.KeyPress
<source lang="csharp"> 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; } }
}
</source>
TextBox.Lines
<source lang="csharp"> 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()); }
}
</source>
TextBox.LostFocus
<source lang="csharp">
using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.Text = "This button does nothing"; 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.MaximizeBox = false; this.Name = "FocusForm"; this.Text = "FocusForm"; this.ResumeLayout(false); } static void Main() { Application.Run(new FocusForm()); } protected void txtFocusForm_LostFocus(object sender, EventArgs e) { // MessageBox.Show("Goodbye!"); } protected void txtFocusForm_GotFocus(object sender, EventArgs e) { // MessageBox.Show("Hello!"); } private void btFocusForm_Click(object sender, System.EventArgs e) { bool canFocus = txtFocusForm.CanFocus; bool containsFocus = this.ContainsFocus; bool focused = txtFocusForm.Focused; MessageBox.Show("Textbox can focus: " + canFocus + "\nForm children contain focus: " + containsFocus + "\nTextbox has focus: " + focused); txtFocusForm.Focus(); } }
</source>
TextBox.MouseDown
<source lang="csharp">
using System; using System.Windows.Forms; public class TextBoxDragDropDemo : Form {
public TextBoxDragDropDemo() { InitializeComponent(); } private void TextBox_MouseDown(object sender, MouseEventArgs e) { TextBox txt = (TextBox)sender; txt.SelectAll(); txt.DoDragDrop(txt.Text, DragDropEffects.Copy); } private void TextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { TextBox txt = (TextBox)sender; txt.Text = (string)e.Data.GetData(DataFormats.Text); } [STAThread] public static void Main(string[] args) { Application.Run(new TextBoxDragDropDemo()); } private System.Windows.Forms.TextBox TextBox2; private System.Windows.Forms.TextBox TextBox1; private void InitializeComponent() { this.TextBox2 = new System.Windows.Forms.TextBox(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.TextBox2.AllowDrop = true; this.TextBox2.Location = new System.Drawing.Point(28, 129); this.TextBox2.Multiline = true; this.TextBox2.Size = new System.Drawing.Size(196, 77); this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); this.TextBox1.AllowDrop = true; this.TextBox1.Location = new System.Drawing.Point(28, 36); this.TextBox1.Multiline = true; this.TextBox1.Size = new System.Drawing.Size(196, 77); this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); 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.TextBox2); this.Controls.Add(this.TextBox1); this.ResumeLayout(false); this.PerformLayout(); }
}
</source>
TextBox.Multiline
<source lang="csharp">
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class TextForm : System.Windows.Forms.Form { 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 TextForm() { InitializeComponent(); } 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.capsOnlyBox.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper; this.capsOnlyBox.Location = new System.Drawing.Point(14, 176); this.capsOnlyBox.Size = new System.Drawing.Size(120, 20); this.capsOnlyBox.TabIndex = 3; this.multiLineBox.AcceptsReturn = true; this.multiLineBox.AcceptsTab = true; this.multiLineBox.Location = new System.Drawing.Point(152, 8); this.multiLineBox.Multiline = true; this.multiLineBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.multiLineBox.Size = new System.Drawing.Size(240, 104); this.multiLineBox.TabIndex = 0; this.multiLineBox.Text = "Get text from multiline textbox"; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F); this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Size = new System.Drawing.Size(136, 56); this.label1.TabIndex = 1; this.label1.Text = "this is a multiline text box"; this.label2.Font = new System.Drawing.Font("Comic Sans MS", 12F); this.label2.Location = new System.Drawing.Point(14, 144); this.label2.Size = new System.Drawing.Size(106, 24); this.label2.TabIndex = 4; this.label2.Text = "Caps only!!"; this.passwordBox.Location = new System.Drawing.Point(160, 176); this.passwordBox.PasswordChar = "$"; this.passwordBox.Size = new System.Drawing.Size(232, 20); this.passwordBox.TabIndex = 5; this.passwordBox.Text = "password"; this.btnGetMultiLineText.Location = new System.Drawing.Point(13, 72); 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); this.btnPasswordDecoderRing.Location = new System.Drawing.Point(280, 144); 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); this.label3.Font = new System.Drawing.Font("Comic Sans MS", 12F); this.label3.Location = new System.Drawing.Point(152, 144); this.label3.Size = new System.Drawing.Size(120, 24); this.label3.TabIndex = 6; this.label3.Text = "Password Box"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(408, 221); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnPasswordDecoderRing, this.label3, this.passwordBox, this.label2, this.capsOnlyBox, this.btnGetMultiLineText, this.label1, this.multiLineBox}); this.Text = "TextBox Types"; } 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 TextForm()); } } </source>
TextBox.PasswordChar
<source lang="csharp">
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class TextForm : System.Windows.Forms.Form { 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 TextForm() { InitializeComponent(); } 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.capsOnlyBox.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper; this.capsOnlyBox.Location = new System.Drawing.Point(14, 176); this.capsOnlyBox.Size = new System.Drawing.Size(120, 20); this.capsOnlyBox.TabIndex = 3; this.multiLineBox.AcceptsReturn = true; this.multiLineBox.AcceptsTab = true; this.multiLineBox.Location = new System.Drawing.Point(152, 8); this.multiLineBox.Multiline = true; this.multiLineBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.multiLineBox.Size = new System.Drawing.Size(240, 104); this.multiLineBox.TabIndex = 0; this.multiLineBox.Text = "Get text from multiline textbox"; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F); this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Size = new System.Drawing.Size(136, 56); this.label1.TabIndex = 1; this.label1.Text = "this is a multiline text box"; this.label2.Font = new System.Drawing.Font("Comic Sans MS", 12F); this.label2.Location = new System.Drawing.Point(14, 144); this.label2.Size = new System.Drawing.Size(106, 24); this.label2.TabIndex = 4; this.label2.Text = "Caps only!!"; this.passwordBox.Location = new System.Drawing.Point(160, 176); this.passwordBox.PasswordChar = "$"; this.passwordBox.Size = new System.Drawing.Size(232, 20); this.passwordBox.TabIndex = 5; this.passwordBox.Text = "password"; this.btnGetMultiLineText.Location = new System.Drawing.Point(13, 72); 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); this.btnPasswordDecoderRing.Location = new System.Drawing.Point(280, 144); 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); this.label3.Font = new System.Drawing.Font("Comic Sans MS", 12F); this.label3.Location = new System.Drawing.Point(152, 144); this.label3.Size = new System.Drawing.Size(120, 24); this.label3.TabIndex = 6; this.label3.Text = "Password Box"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(408, 221); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnPasswordDecoderRing, this.label3, this.passwordBox, this.label2, this.capsOnlyBox, this.btnGetMultiLineText, this.label1, this.multiLineBox}); this.Text = "TextBox Types"; } 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 TextForm()); } } </source>
TextBox.ScrollBars
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class NotepadCloneNoMenu: Form {
protected TextBox txtbox; public static void Main() { Application.Run(new NotepadCloneNoMenu()); } public NotepadCloneNoMenu() { Text = "Notepad Clone No Menu"; txtbox = new TextBox(); txtbox.Parent = this; txtbox.Dock = DockStyle.Fill; txtbox.BorderStyle = BorderStyle.None; txtbox.Multiline = true; txtbox.ScrollBars = ScrollBars.Both; txtbox.AcceptsTab = true; }
}
</source>
TextBox.SelectAll()
<source lang="csharp">
using System; using System.Windows.Forms; public class TextBoxDragDropDemo : Form {
public TextBoxDragDropDemo() { InitializeComponent(); } private void TextBox_MouseDown(object sender, MouseEventArgs e) { TextBox txt = (TextBox)sender; txt.SelectAll(); txt.DoDragDrop(txt.Text, DragDropEffects.Copy); } private void TextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { TextBox txt = (TextBox)sender; txt.Text = (string)e.Data.GetData(DataFormats.Text); } [STAThread] public static void Main(string[] args) { Application.Run(new TextBoxDragDropDemo()); } private System.Windows.Forms.TextBox TextBox2; private System.Windows.Forms.TextBox TextBox1; private void InitializeComponent() { this.TextBox2 = new System.Windows.Forms.TextBox(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.TextBox2.AllowDrop = true; this.TextBox2.Location = new System.Drawing.Point(28, 129); this.TextBox2.Multiline = true; this.TextBox2.Size = new System.Drawing.Size(196, 77); this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); this.TextBox1.AllowDrop = true; this.TextBox1.Location = new System.Drawing.Point(28, 36); this.TextBox1.Multiline = true; this.TextBox1.Size = new System.Drawing.Size(196, 77); this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown); 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.TextBox2); this.Controls.Add(this.TextBox1); this.ResumeLayout(false); this.PerformLayout(); }
}
</source>
TextBox.SelectedText
<source lang="csharp"> 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()); }
}
</source>
TextBox.SelectionLength
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Text; public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Label label2; public Form1() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label1 = new System.Windows.Forms.Label(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.label2 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(16, 24); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(168, 20); this.textBox1.TabIndex = 5; this.textBox1.Text = ""; this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown); // // textBox2 // this.textBox2.Location = new System.Drawing.Point(16, 24); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(168, 20); this.textBox2.TabIndex = 6; this.textBox2.Text = ""; this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.label1, this.textBox1}); this.groupBox1.Location = new System.Drawing.Point(8, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.TabIndex = 7; this.groupBox1.TabStop = false; this.groupBox1.Text = "Key Monitor"; // // label1 // this.label1.Location = new System.Drawing.Point(16, 64); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(168, 20); this.label1.TabIndex = 6; // // groupBox2 // this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox2, this.label2}); this.groupBox2.Location = new System.Drawing.Point(8, 120); this.groupBox2.Name = "groupBox2"; this.groupBox2.TabIndex = 8; this.groupBox2.TabStop = false; this.groupBox2.Text = "Keys Enumeration"; // // label2 // this.label2.Location = new System.Drawing.Point(16, 64); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(168, 20); this.label2.TabIndex = 9; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(216, 229); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox2, this.groupBox1}); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { label1.Text = Convert.ToString(e.KeyValue); } private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.Shift) Console.WriteLine("Shift, "); if (e.Alt) Console.WriteLine("Alt, "); if (e.Control) Console.WriteLine("Ctrl, "); if (e.KeyCode == Keys.W || e.KeyCode == Keys.R ) { Console.WriteLine("W R "); } else if (e.KeyCode == Keys.Escape && e.Modifiers == (Keys.Shift | Keys.Alt)) { Console.WriteLine("Escape"); } else if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Alt | Keys.Control)) { Console.WriteLine("s"); textBox2.SelectedText = ""; textBox2.SelectionLength = 0; } else { Console.WriteLine(Convert.ToString(e.KeyData)); } }
}
</source>
TextBox.TextChanged
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {
private System.Windows.Forms.GroupBox GroupBox1; private System.Windows.Forms.Label Label4; private System.Windows.Forms.Label Label1; private System.Windows.Forms.PictureBox pic; private System.Windows.Forms.TextBox txt; private System.Windows.Forms.Button cmd; private System.Windows.Forms.Label Label2; private System.Windows.Forms.Label Label3; private System.Windows.Forms.ListBox eventLogList; public Form1() { InitializeComponent(); } private void Log(String data) { eventLogList.Items.Add(data); int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight); eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage; } private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString()); } private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { Log("Key Press: " + e.KeyChar.ToString()); } private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text); } private void txt_TextChanged(object sender, System.EventArgs e) { Log("Changed: " + " Text is: " + txt.Text); } private void pic_MouseEnter(object sender, System.EventArgs e) { Log("Mouse Enter"); } private void pic_MouseHover(object sender, System.EventArgs e) { Log("Mouse Hover"); } private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString()); } private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString()); } private void pic_Click(object sender, System.EventArgs e) { Log("Click"); } private void pic_DoubleClick(object sender, System.EventArgs e) { Log("Double Click"); } private void pic_MouseLeave(object sender, System.EventArgs e) { Log("Mouse Leave"); } private void InitializeComponent() { this.GroupBox1 = new System.Windows.Forms.GroupBox(); this.Label4 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.pic = new System.Windows.Forms.PictureBox(); this.txt = new System.Windows.Forms.TextBox(); this.cmd = new System.Windows.Forms.Button(); this.Label2 = new System.Windows.Forms.Label(); this.Label3 = new System.Windows.Forms.Label(); this.eventLogList = new System.Windows.Forms.ListBox(); this.GroupBox1.SuspendLayout(); ((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit(); this.SuspendLayout(); // // GroupBox1 // this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.GroupBox1.Controls.Add(this.Label4); this.GroupBox1.Controls.Add(this.Label1); this.GroupBox1.Controls.Add(this.pic); this.GroupBox1.Controls.Add(this.txt); this.GroupBox1.Controls.Add(this.cmd); this.GroupBox1.Controls.Add(this.Label2); this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System; this.GroupBox1.Location = new System.Drawing.Point(7, 0); this.GroupBox1.Name = "GroupBox1"; this.GroupBox1.Size = new System.Drawing.Size(384, 148); this.GroupBox1.TabIndex = 12; this.GroupBox1.TabStop = false; // // Label4 // this.Label4.Location = new System.Drawing.Point(92, 108); this.Label4.Name = "Label4"; this.Label4.Size = new System.Drawing.Size(56, 16); this.Label4.TabIndex = 5; this.Label4.Text = "And here:"; // // Label1 // this.Label1.Location = new System.Drawing.Point(6, 24); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(144, 16); this.Label1.TabIndex = 2; this.Label1.Text = "Test keyboard events here:"; // // pic // this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.pic.Location = new System.Drawing.Point(156, 48); this.pic.Name = "pic"; this.pic.Size = new System.Drawing.Size(192, 48); this.pic.TabIndex = 3; this.pic.TabStop = false; this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick); this.pic.Click += new System.EventHandler(this.pic_Click); this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover); this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp); this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter); this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown); // // txt // this.txt.Location = new System.Drawing.Point(156, 20); this.txt.Name = "txt"; this.txt.Size = new System.Drawing.Size(192, 21); this.txt.TabIndex = 1; this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp); this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress); this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged); this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown); // // cmd // this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmd.Location = new System.Drawing.Point(156, 100); this.cmd.Name = "cmd"; this.cmd.Size = new System.Drawing.Size(88, 28); this.cmd.TabIndex = 4; this.cmd.Text = "Button1"; this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave); this.cmd.Click += new System.EventHandler(this.pic_Click); this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter); this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover); this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp); this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown); // // Label2 // this.Label2.Location = new System.Drawing.Point(20, 52); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(128, 16); this.Label2.TabIndex = 2; this.Label2.Text = "Test mouse events here:"; // // Label3 // this.Label3.Location = new System.Drawing.Point(23, 100); this.Label3.Name = "Label3"; this.Label3.Size = new System.Drawing.Size(64, 24); this.Label3.TabIndex = 11; this.Label3.Text = "Label3"; // // eventLogList // this.eventLogList.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.eventLogList.FormattingEnabled = true; this.eventLogList.IntegralHeight = false; this.eventLogList.Location = new System.Drawing.Point(7, 156); this.eventLogList.Name = "eventLogList"; this.eventLogList.Size = new System.Drawing.Size(384, 212); this.eventLogList.TabIndex = 10; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(399, 374); this.Controls.Add(this.GroupBox1); this.Controls.Add(this.Label3); this.Controls.Add(this.eventLogList); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "Form1"; this.Text = "Event Tracker"; this.GroupBox1.ResumeLayout(false); this.GroupBox1.PerformLayout(); ((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit(); this.ResumeLayout(false); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}
</source>
TextBox.Validated
<source lang="csharp">
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()); }
}
</source>
TextBox.Validating
<source lang="csharp"> 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()); }
}
</source>