0% found this document useful (0 votes)
6 views15 pages

TextBox Properties

The document provides examples of TextBox properties, events, and methods in C#, including how to validate input and provide visual feedback using an ErrorProvider. It demonstrates the use of event handlers for validating multiple TextBox controls and changing their background color to indicate invalid input. Additionally, it shows how to centralize validation logic and customize visual cues based on application requirements.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

TextBox Properties

The document provides examples of TextBox properties, events, and methods in C#, including how to validate input and provide visual feedback using an ErrorProvider. It demonstrates the use of event handlers for validating multiple TextBox controls and changing their background color to indicate invalid input. Additionally, it shows how to centralize validation logic and customize visual cues based on application requirements.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Handout Three: TextBox Properties, Events, and Methods Examples

Here are some examples of TextBox properties, events, and methods in C#:

Properties:

Text: Gets or sets the text content of the TextBox control.

TextBox textBox = new TextBox();

textBox.Text = "Hello, World!";

Enabled: Gets or sets a value indicating whether the TextBox control is enabled.

TextBox textBox = new TextBox();

textBox.Enabled = false;

Multiline: Gets or sets a value indicating whether the TextBox control allows multiple lines of text.

TextBox textBox = new TextBox();

textBox.Multiline = true;

MaxLength: Gets or sets the maximum number of characters that can be entered into the TextBox
control.

TextBox textBox = new TextBox();

textBox.MaxLength = 100;

ReadOnly: Gets or sets a value indicating whether the TextBox control is read-only.

TextBox textBox = new TextBox();

textBox.ReadOnly = true;

Events:

TextChanged: Occurs when the text content of the TextBox control changes.

TextBox textBox = new TextBox();

textBox.TextChanged += TextBox_TextChanged;

private void TextBox_TextChanged(object sender, EventArgs e)

// Text changed event handler

Dr. Ahmed Alnasheri 1/15


KeyPress: Occurs when a key is pressed while the TextBox control has focus.

TextBox textBox = new TextBox();

textBox.KeyPress += TextBox_KeyPress;

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)

// Key press event handler

LostFocus: Occurs when the TextBox control loses focus.

TextBox textBox = new TextBox();

textBox.LostFocus += TextBox_LostFocus;

private void TextBox_LostFocus(object sender, EventArgs e)

// Lost focus event handler

Validating: Occurs when the TextBox control is validating its content.

TextBox textBox = new TextBox();

textBox.Validating += TextBox_Validating;

private void TextBox_Validating(object sender, CancelEventArgs e)

// Validating event handler

Methods:

Clear(): Clears the text content of the TextBox control.

TextBox textBox = new TextBox();

textBox.Clear();

SelectAll(): Selects all the text in the TextBox control.

Dr. Ahmed Alnasheri 2/15


TextBox textBox = new TextBox();

textBox.SelectAll();

Copy(): Copies the selected text to the clipboard.

TextBox textBox = new TextBox();

textBox.Copy();

Cut(): Cuts the selected text to the clipboard.

TextBox textBox = new TextBox();

textBox.Cut();

Paste(): Pastes the contents of the clipboard into the TextBox control.

TextBox textBox = new TextBox();

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 class MainForm : Form

private TextBox textBox;

private ErrorProvider errorProvider;

public MainForm()

// Create the TextBox control

textBox = new TextBox();

textBox.Location = new System.Drawing.Point(50, 50);

// Create the ErrorProvider control

errorProvider = new ErrorProvider();

errorProvider.SetIconAlignment(textBox, ErrorIconAlignment.MiddleRight);

Dr. Ahmed Alnasheri 3/15


// Set the event handler for the Validating event

textBox.Validating += TextBox_Validating;

// Add the TextBox control to the form

Controls.Add(textBox);

private void TextBox_Validating(object sender, CancelEventArgs e)

string input = textBox.Text;

// Perform validation logic

if (string.IsNullOrWhiteSpace(input))

// Set the error message and cancel the event

errorProvider.SetError(textBox, "Input is required.");

e.Cancel = true;

else

// Clear the error message

errorProvider.SetError(textBox, string.Empty);

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.Run(new MainForm());

Dr. Ahmed Alnasheri 4/15


}

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 class MainForm : Form

private TextBox textBox1;

private TextBox textBox2;

private ErrorProvider errorProvider;

public MainForm()

// Create the TextBox controls

textBox1 = new TextBox();

textBox1.Location = new System.Drawing.Point(50, 50);

Dr. Ahmed Alnasheri 5/15


textBox2 = new TextBox();

textBox2.Location = new System.Drawing.Point(50, 100);

// Create the ErrorProvider control

errorProvider = new ErrorProvider();

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;

// Add the TextBox controls to the form

Controls.Add(textBox1);

Controls.Add(textBox2);

private void TextBox_Validating(object sender, CancelEventArgs e)

TextBox textBox = (TextBox)sender;

string input = textBox.Text;

// Perform validation logic for each TextBox control

if (string.IsNullOrWhiteSpace(input))

// Set the error message and cancel the event

errorProvider.SetError(textBox, "Input is required.");

e.Cancel = true;

else

// Clear the error message

Dr. Ahmed Alnasheri 6/15


errorProvider.SetError(textBox, string.Empty);

[STAThread]

static void Main()

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.

Here's an example that demonstrates how to achieve this:

using System;

using System.ComponentModel;

using System.Windows.Forms;

public class MainForm : Form

private TextBox textBox1;

private TextBox textBox2;

private Button validateButton;

private ErrorProvider errorProvider;

Dr. Ahmed Alnasheri 7/15


public MainForm()

// Create the TextBox controls

textBox1 = new TextBox();

textBox1.Location = new System.Drawing.Point(50, 50);

textBox2 = new TextBox();

textBox2.Location = new System.Drawing.Point(50, 100);

// Create the validate button

validateButton = new Button();

validateButton.Location = new System.Drawing.Point(50, 150);

validateButton.Text = "Validate";

validateButton.Click += ValidateButton_Click;

// Create the ErrorProvider control

errorProvider = new ErrorProvider();

errorProvider.SetIconAlignment(textBox1, ErrorIconAlignment.MiddleRight);

errorProvider.SetIconAlignment(textBox2, ErrorIconAlignment.MiddleRight);

// Add the controls to the form

Controls.Add(textBox1);

Controls.Add(textBox2);

Controls.Add(validateButton);

private void ValidateButton_Click(object sender, EventArgs e)

// Perform validation for each TextBox control

ValidateTextBox(textBox1);

ValidateTextBox(textBox2);

private void ValidateTextBox(TextBox textBox)

Dr. Ahmed Alnasheri 8/15


string input = textBox.Text;

// Perform validation logic for each TextBox control

if (string.IsNullOrWhiteSpace(input))

// Set the error message

errorProvider.SetError(textBox, "Input is required.");

else

// Clear the error message

errorProvider.SetError(textBox, string.Empty);

[STAThread]

static void Main()

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.

Dr. Ahmed Alnasheri 9/15


Besides that, it is possible to highlight the TextBox controls with invalid input. One way to achieve this is
by changing the visual appearance of the TextBox controls when they fail validation. Here's an example
that demonstrates how to highlight TextBox controls with invalid input using background color:

using System;

using System.ComponentModel;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

private TextBox textBox1;

private TextBox textBox2;

private Button validateButton;

public MainForm()

// Create the TextBox controls

textBox1 = new TextBox();

textBox1.Location = new System.Drawing.Point(50, 50);

textBox2 = new TextBox();

textBox2.Location = new System.Drawing.Point(50, 100);

// Create the validate button

validateButton = new Button();

validateButton.Location = new System.Drawing.Point(50, 150);

validateButton.Text = "Validate";

validateButton.Click += ValidateButton_Click;

Dr. Ahmed Alnasheri 10/15


// Add the controls to the form

Controls.Add(textBox1);

Controls.Add(textBox2);

Controls.Add(validateButton);

private void ValidateButton_Click(object sender, EventArgs e)

// Reset the background color of all TextBox controls

ResetTextBoxBackgroundColor(textBox1);

ResetTextBoxBackgroundColor(textBox2);

// Perform validation for each TextBox control

ValidateTextBox(textBox1);

ValidateTextBox(textBox2);

private void ValidateTextBox(TextBox textBox)

string input = textBox.Text;

// Perform validation logic for each TextBox control

if (string.IsNullOrWhiteSpace(input))

// Set the background color to indicate invalid input

textBox.BackColor = Color.LightPink;

Dr. Ahmed Alnasheri 11/15


private void ResetTextBoxBackgroundColor(TextBox textBox)

// Reset the background color to the default

textBox.BackColor = SystemColors.Window;

[STAThread]

static void Main()

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;

Dr. Ahmed Alnasheri 12/15


public class MainForm : Form

private TextBox textBox1;

private TextBox textBox2;

private Button validateButton;

public MainForm()

// Create the TextBox controls

textBox1 = new TextBox();

textBox1.Location = new System.Drawing.Point(50, 50);

textBox2 = new TextBox();

textBox2.Location = new System.Drawing.Point(50, 100);

// Create the validate button

validateButton = new Button();

validateButton.Location = new System.Drawing.Point(50, 150);

validateButton.Text = "Validate";

validateButton.Click += ValidateButton_Click;

// Add the controls to the form

Controls.Add(textBox1);

Controls.Add(textBox2);

Controls.Add(validateButton);

private void ValidateButton_Click(object sender, EventArgs e)

Dr. Ahmed Alnasheri 13/15


// Reset the background color of all TextBox controls

ResetTextBoxBackgroundColor(textBox1);

ResetTextBoxBackgroundColor(textBox2);

// Perform validation for each TextBox control

ValidateTextBox(textBox1);

ValidateTextBox(textBox2);

private void ValidateTextBox(TextBox textBox)

string input = textBox.Text;

// Perform validation logic for each TextBox control

if (string.IsNullOrWhiteSpace(input))

// Set the background color to indicate invalid input

textBox.BackColor = Color.LightBlue;

private void ResetTextBoxBackgroundColor(TextBox textBox)

// Reset the background color to the default

textBox.BackColor = SystemColors.Window;

[STAThread]

static void Main()

Dr. Ahmed Alnasheri 14/15


{

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.

Dr. Ahmed Alnasheri 15/15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy