Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace TextBoxTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- this.btnOK.Enabled = true;
- // Tag values for testing if the data is valid
- this.txtAddress.Tag = false;
- this.txtAge.Tag = false;
- this.txtName.Tag = false;
- this.txtOccupation.Tag = false;
- // Subscriptions to events
- this.txtName.Validating += new
- System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
- this.txtAddress.Validating += new
- System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
- this.txtOccupation.Validating += new
- System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);
- this.txtAge.KeyPress += new
- System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);
- this.txtAge.Validating += new
- System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
- this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
- this.txtAddress.TextChanged += new
- System.EventHandler(this.txtBox_TextChanged);
- this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
- this.txtOccupation.TextChanged += new
- System.EventHandler(this.txtBox_TextChanged);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void textBox5_TextChanged(object sender, EventArgs e)
- {
- }
- private void btnOK_Click(object sender, System.EventArgs e)
- {
- // No testing for invalid values are made, as that should
- // not be necessary
- string output;
- // Concatenate the text values of the four TextBoxes
- output = "Name: " + this.txtName.Text + "\r\n";
- output += "Address: " + this.txtAddress.Text + "\r\n";
- output += "Occupation: " + this.txtOccupation.Text + "\r\n";
- output += "Age: " + this.txtAge.Text;
- // Insert the new text
- this.txtOutput.Text = output;
- }
- private void textBox3_TextChanged(object sender, EventArgs e)
- {
- }
- private void btnHelp_Click(object sender, System.EventArgs e)
- {
- // Write a short description of each TextBox in the Output TextBox
- string output;
- output = "Name = Your name\r\n";
- output += "Address = Your address\r\n";
- output += "Occupation = Only allowed value is 'Programmer'\r\n";
- output += "Age = Your age";
- // Insert the new text
- this.txtOutput.Text = output;
- }
- private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
- {
- // Cast the sender object to a textbox
- TextBox tb = (TextBox)sender;
- // Check if the values are correct
- 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;
- }
- // Set the state of the OK button
- ValidateAll();
- }
- private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
- {
- // We know the sender is a TextBox, so we cast the sender object to that
- TextBox tb = (TextBox)sender;
- // If the text is empty we set the background color of the
- // Textbox to red to indicate a problem. We use the tag value
- // of the control to indicate if the control contains valid
- // information.
- if (tb.Text.Length == 0)
- {
- tb.BackColor = Color.Red;
- tb.Tag = false;
- // In this case we do not want to cancel further processing,
- // but if we had wanted to do this, we would have added this line:
- // e.Cancel = true;
- }
- else
- {
- tb.BackColor = System.Drawing.SystemColors.Window;
- tb.Tag = true;
- }
- // Finally, we call ValidateAll which will set the value of
- // the OK button.
- ValidateAll();
- }
- private void txtAge_KeyPress(object sender,
- System.Windows.Forms.KeyPressEventArgs e)
- {
- if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
- e.Handled = true; // Remove the character
- }
- private void txtBox_TextChanged(object sender, System.EventArgs e)
- {
- // Cast the sender object to a Textbox
- TextBox tb = (TextBox)sender;
- // Test if the data is valid and set the tag and background
- // color accordingly.
- if (tb.Text.Length == 0 && tb != txtOccupation)
- {
- tb.Tag = false;
- tb.BackColor = Color.Red;
- }
- else if (tb == txtOccupation &&
- (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0))
- {
- // Don't set the color here, as it will color change while the user
- // is typing
- tb.Tag = false;
- }
- else
- {
- tb.Tag = true;
- tb.BackColor = SystemColors.Window;
- }
- // Call ValidateAll to set the OK button
- ValidateAll();
- }
- private void ValidateAll()
- {
- // Set the OK button to enabled if all the Tags are true
- this.btnOK.Enabled = ((bool)(this.txtAddress.Tag) &&
- (bool)(this.txtAge.Tag) &&
- (bool)(this.txtName.Tag) &&
- (bool)(this.txtOccupation.Tag));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement