TextBox Properties
TextBox Properties
Here are some examples of TextBox properties, events, and methods in C#:
Properties:
Enabled: Gets or sets a value indicating whether the TextBox control is enabled.
textBox.Enabled = false;
Multiline: Gets or sets a value indicating whether the TextBox control allows multiple lines of text.
textBox.Multiline = true;
MaxLength: Gets or sets the maximum number of characters that can be entered into the TextBox
control.
textBox.MaxLength = 100;
ReadOnly: Gets or sets a value indicating whether the TextBox control is read-only.
textBox.ReadOnly = true;
Events:
TextChanged: Occurs when the text content of the TextBox control changes.
textBox.TextChanged += TextBox_TextChanged;
textBox.KeyPress += TextBox_KeyPress;
textBox.LostFocus += TextBox_LostFocus;
textBox.Validating += TextBox_Validating;
Methods:
textBox.Clear();
textBox.SelectAll();
textBox.Copy();
textBox.Cut();
Paste(): Pastes the contents of the clipboard into the TextBox control.
textBox.Paste();
Here's an example that demonstrates how to use the Validating event of a TextBox control in C#:
using System;
using System.ComponentModel;
using System.Windows.Forms;
public MainForm()
errorProvider.SetIconAlignment(textBox, ErrorIconAlignment.MiddleRight);
textBox.Validating += TextBox_Validating;
Controls.Add(textBox);
if (string.IsNullOrWhiteSpace(input))
e.Cancel = true;
else
errorProvider.SetError(textBox, string.Empty);
[STAThread]
Application.EnableVisualStyles();
Application.Run(new MainForm());
In this example, we create a MainForm class that inherits from Form. Inside the constructor, we initialize
a TextBox control and an ErrorProvider control.
We then associate the Validating event of the TextBox control with the TextBox_Validating event
handler. The Validating event occurs when the TextBox control is validating its content.
In the TextBox_Validating event handler, we perform the validation logic. In this case, we check if the
input is empty or consists only of whitespace characters. If it does, we set an error message using the
ErrorProvider control and cancel the event by setting e.Cancel to true. This prevents the focus from
leaving the TextBox control until the validation condition is met. If the input is valid, we clear the error
message.
By using the Validating event and the ErrorProvider control, you can provide visual feedback to the user
when the content of the TextBox control fails validation.
It's certainly possible to validate multiple TextBox controls using the same event handler.
Here's an example that demonstrates how to validate multiple TextBox controls using a single event
handler:
using System;
using System.ComponentModel;
using System.Windows.Forms;
public MainForm()
errorProvider.SetIconAlignment(textBox1, ErrorIconAlignment.MiddleRight);
errorProvider.SetIconAlignment(textBox2, ErrorIconAlignment.MiddleRight);
// Set the event handler for the Validating event for both TextBox controls
textBox1.Validating += TextBox_Validating;
textBox2.Validating += TextBox_Validating;
Controls.Add(textBox1);
Controls.Add(textBox2);
if (string.IsNullOrWhiteSpace(input))
e.Cancel = true;
else
[STAThread]
Application.EnableVisualStyles();
Application.Run(new MainForm());
In this example, we create two TextBox controls (textBox1 and textBox2) and associate the same
Validating event handler, TextBox_Validating, with both of them.
Inside the TextBox_Validating event handler, we cast the sender parameter to a TextBox to determine
which TextBox control triggered the event. Then, we perform the validation logic for each TextBox
control individually. In this case, we check if the input is empty or consists only of whitespace characters.
If it does, we set an error message using the ErrorProvider control and cancel the event by setting
e.Cancel to true. If the input is valid, we clear the error message.
By using a single event handler for multiple TextBox controls, you can centralize the validation logic and
avoid duplicating code.
In addition, it is possible to validate the TextBox controls only when a button is clicked.
using System;
using System.ComponentModel;
using System.Windows.Forms;
validateButton.Text = "Validate";
validateButton.Click += ValidateButton_Click;
errorProvider.SetIconAlignment(textBox1, ErrorIconAlignment.MiddleRight);
errorProvider.SetIconAlignment(textBox2, ErrorIconAlignment.MiddleRight);
Controls.Add(textBox1);
Controls.Add(textBox2);
Controls.Add(validateButton);
ValidateTextBox(textBox1);
ValidateTextBox(textBox2);
if (string.IsNullOrWhiteSpace(input))
else
errorProvider.SetError(textBox, string.Empty);
[STAThread]
Application.EnableVisualStyles();
Application.Run(new MainForm());
In this example, we create two TextBox controls (textBox1 and textBox2), a Button (validateButton), and
an ErrorProvider control (errorProvider).
The validation logic is performed when the validateButton is clicked, which is handled by the
ValidateButton_Click event handler. Inside this event handler, we call the ValidateTextBox method for
each TextBox control that needs to be validated.
The ValidateTextBox method performs the actual validation logic for a given TextBox control. In this
case, we check if the input is empty or consists only of whitespace characters. If it does, we set an error
message using the ErrorProvider control. If the input is valid, we clear the error message.
By validating the TextBox controls only when the button is clicked, you can control the timing and
conditions for triggering the validation process based on your specific requirements.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public MainForm()
validateButton.Text = "Validate";
validateButton.Click += ValidateButton_Click;
Controls.Add(textBox1);
Controls.Add(textBox2);
Controls.Add(validateButton);
ResetTextBoxBackgroundColor(textBox1);
ResetTextBoxBackgroundColor(textBox2);
ValidateTextBox(textBox1);
ValidateTextBox(textBox2);
if (string.IsNullOrWhiteSpace(input))
textBox.BackColor = Color.LightPink;
textBox.BackColor = SystemColors.Window;
[STAThread]
Application.EnableVisualStyles();
Application.Run(new MainForm());
In this example, we introduce a ResetTextBoxBackgroundColor method that resets the background color
of a TextBox control to the default color (SystemColors.Window). This method is called before
performing validation to ensure a clean state.
Inside the ValidateTextBox method, if the validation fails (in this case, if the input is empty or consists
only of whitespace characters), we set the background color of the TextBox control to indicate invalid
input (Color.LightPink).
By changing the background color, you can visually highlight the TextBox controls with invalid input. You
can customize the highlighting effect by using different colors or additional visual cues, such as borders
or icons, depending on your application's design and requirements.
You can change the color to something other than LightPink to indicate invalid input. You can use any
valid Color value that suits your application's design and requirements. Here's an updated example that
demonstrates how to change the background color to a different color:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public MainForm()
validateButton.Text = "Validate";
validateButton.Click += ValidateButton_Click;
Controls.Add(textBox1);
Controls.Add(textBox2);
Controls.Add(validateButton);
ResetTextBoxBackgroundColor(textBox1);
ResetTextBoxBackgroundColor(textBox2);
ValidateTextBox(textBox1);
ValidateTextBox(textBox2);
if (string.IsNullOrWhiteSpace(input))
textBox.BackColor = Color.LightBlue;
textBox.BackColor = SystemColors.Window;
[STAThread]
Application.EnableVisualStyles();
Application.Run(new MainForm());
In this updated example, we've changed the background color to Color.LightBlue to indicate invalid
input. You can replace Color.LightBlue with any other valid Color value of your choice. You can use
named colors (e.g., Color.Red, Color.Yellow) or specify custom colors using RGB values (e.g.,
Color.FromArgb(255, 0, 0) for pure red).
Feel free to experiment with different colors until you find the one that best suits your application's
visual style and serves as an effective indicator of invalid input.