Example 1: Here is a first very, very simple example:
When the user clicks the button, the text “click button” is placed in the textbox.
Here is the code to perform this operation:
1 2 3 4 5 6 | private void btn1_Click(object sender, EventArgs e) { txt1.Text = "Click Button"; } |
btn1 is the name of the button, txt1 is the name of the textBox.
If we look at the part of code concerning the creation of the window, code generated by the environment, here is what we find:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | namespace WindowsFormsApp14 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (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.btn1 = new System.Windows.Forms.Button(); this.txt1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btn1 // this.btn1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162))); this.btn1.Location = new System.Drawing.Point(143, 130); this.btn1.Name = "btn1"; this.btn1.Size = new System.Drawing.Size(153, 36); this.btn1.TabIndex = 0; this.btn1.Text = "Click Button"; this.btn1.UseVisualStyleBackColor = true; this.btn1.Click += new System.EventHandler(this.button1_Click); // // txt1 // this.txt1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162))); this.txt1.Location = new System.Drawing.Point(143, 60); this.txt1.Name = "txt1"; this.txt1.Size = new System.Drawing.Size(153, 31); this.txt1.TabIndex = 1; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(434, 230); this.Controls.Add(this.txt1); this.Controls.Add(this.btn1); this.Name = "Form1"; this.Text = "csharp-console-examples.com"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btn1; private System.Windows.Forms.TextBox txt1; } } |
Example 2: Another example always very simple:
When the user clicks on the button, the text “click button” is placed in the textBox:
1 2 3 4 5 6 | private void btn1_Click (object sender, System.EventArgs e) { txt1.Text = "Click Button"; } |
Moreover, when the content of the textBox is “end” the button becomes invisible, otherwise it is visible:
1 2 3 4 5 6 7 8 9 | private void txt1_TextChanged(object sender, System.EventArgs e) { if (txt1.Text.Equals ("end")) btn1.Visible = false; else btn1.Visible = true; } |
This type of writing should preferably be replaced by the equivalent instruction:
1 2 3 4 5 6 | private void txt1_TextChanged(object sender, System.EventArgs e) { btn1.Visible = !txt1.Text.Equals ("end"); } |
Example 3: Change background color by skipping over the textBox with the mouse (and writing the color):
The code takes the two previous examples and adds the background color change:
1 2 3 4 5 6 7 8 9 10 11 12 13 | private void txt1_MouseHover(object sender, EventArgs e) { BackColor = Color.Red; txt1.Text = "Color red"; } private void txt1_MouseLeave(object sender, EventArgs e) { BackColor = Color.Aqua; txt1.Text = "Color aqua"; } |
Example 4: Transfer of information between a textBox and a listBox; select an element in a listBox:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar==(char)13) { listBox1.Items.Add(textBox1.Text); // upload content to the listBox textBox1.Clear(); // dump the textBox e.Handled = true; // indicates that the character has been processed } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { listBox1.Sorted = checkBox1.Checked; // choice of sorting listBox elements } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if(listBox1.SelectedIndex!=-1) { textBox2.Text = listBox1.SelectedItem.ToString(); // transfer content in the textBox } } |