Detailed Explanation of C# TextBox Event Implementation Examples - C# - Programming Development - Eden Network

by henxue on 2010-07-16 13:46:36

Here is the English translation of the provided text:

---

C# TextBox events are specific functional requirements we encounter during development. So, how do we make the desired C# TextBox events execute? Here, I will introduce you to specific examples of C# TextBox event demonstrations, including the implementation of these requirements, which I hope will be helpful to you.

### Specific Requirements for C# TextBox Events:

- **Interface Requirements**: Define 5 TEXTBOX fields: Name, Address, Occupation, Age, and Output Content. Also, include 1 BUTTON.

- **Satisfaction Conditions**:

1. The username cannot be empty.

2. The age must be a number greater than or equal to 0.

3. The occupation must be "Programmer" or left blank.

4. The address cannot be empty.

### Implementation of the C# TextBox Event Example:

```csharp

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Chapter14

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

this.button1.Enabled = false;

this.textBox1.Tag = false;

this.textBox2.Tag = false;

this.textBox3.Tag = false;

this.textBox4.Tag = false;

this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textboxEmpty_Validating);

this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textboxEmpty_Validating);

this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textboxOccupation_Validating);

this.textBox4.Validating += new System.ComponentModel.CancelEventHandler(this.textboxEmpty_Validating);

this.textBox1.TextChanged += new System.EventHandler(this.textbox_TextChanged);

this.textBox4.TextChanged += new System.EventHandler(this.textbox_TextChanged);

this.textBox3.TextChanged += new System.EventHandler(this.textbox_TextChanged);

}

// C# TextBox Event

private void textboxOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)

{

TextBox tb = (TextBox)sender;

if (tb.Text.CompareTo("Programmer") == 0 || tb.Text.Length == 0)

{

tb.Tag = true;

tb.BackColor = System.Drawing.SystemColors.Window;

}

else

{

tb.Tag = false;

tb.BackColor = Color.Red;

}

ValidateOk();

}

// C# TextBox Event

private void textboxEmpty_Validating(object sender, System.ComponentModel.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 57) && e.KeyChar != 8)

e.Handled = true;

}

private void textbox_TextChanged(object sender, System.EventArgs e)

{

TextBox tb = (TextBox)sender;

if (tb.Text.Length == 0 && tb != textBox3)

{

tb.Tag = false;

tb.BackColor = Color.Red;

}

else if (tb == this.textBox3 && (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0))

{

tb.Tag = false;

}

else

{

tb.Tag = true;

tb.BackColor = SystemColors.Window;

}

ValidateOk();

}

private void ValidateOk()

{

this.button1.Enabled = ((bool)(this.textBox2.Tag) && (bool)(this.textBox4.Tag) && (bool)(this.textBox1.Tag) && (bool)(this.textBox3.Tag));

}

private void button1_Click(object sender, EventArgs e)

{

string output;

// C# TextBox Event

output = "Name:" + this.textBox1.Text + " ";

output += "Address:" + this.textBox2.Text + " ";

output += "Occupation:" + this.textBox3.Text + " ";

output += "Age:" + this.textBox4.Text + " ";

this.textBox5.Text = output;

}

}

}

```

### Conclusion

This is the detailed example of implementing C# TextBox events. I hope it helps you understand and learn about C# TextBox events effectively.

Original article link: [Eden Network](http://www.edenw.com/tech/devdeloper/Cp/2010-07-16/4753.html)

---

This translation provides an accurate representation of the original content in English while maintaining the technical details and structure of the code.