Different Windows Control | Text Box | BKNMU Junagadh


 

A TextBox is like a space on a Form where users can type stuff. In this article, I'll show you how to make a TextBox on a Windows Form when you're setting things up and also when the program is running. Then, we'll talk about all the different things you can do with a TextBox, like changing how it looks or using it in different ways.

Creating a TextBox

You have two ways to make a TextBox. First, you can use a Forms designer when you're getting things ready. Second, you can use the TextBox class in your code while the program is running, which is also called creating it dynamically.


Creating a TextBox control at design-time is easy. Just grab the TextBox from the Toolbox and drop it onto your Form in Visual Studio. When you do this, the TextBox will appear, as shown in Figure 1. Once it's on the Form, you can use your mouse to move it, make it bigger or smaller, and tweak its settings and actions.


To make a dynamic TextBox, start by making a TextBox class instance. The code below is an example of how to create a TextBox control object:
TextBox dynamicTextBox = newTextBox();

In the next step, you can adjust the properties of a TextBox control. Check out the code below, which sets the background color, foreground color, text content, name, and font properties of a TextBox:

dynamicTextBox.BackColor = Color.Red;  
dynamicTextBox.ForeColor = Color.Blue;  
dynamicTextBox.Text = "I am Dynamic TextBox";  
dynamicTextBox.Name = "DynamicTextBox";  
dynamicTextBox.Font = newFont("Georgia", 16);    

After setting up the TextBox control with its properties, the next step is to add it to the Form. You can do this using the Form.Controls.Add method. Check out the code below, which adds a TextBox control to the current Form:

Controls.Add(dynamicTextBox);

  
This line of code places your dynamic TextBox onto the Form, making it a part of the user interface.


Setting TextBox Properties


Once you've added a TextBox control to a Form, the next step is to tweak its properties.


The simplest way to do this is through the Properties Window. You can open the Properties window by pressing F4 or by right-clicking on the control and selecting the Properties menu item. The Properties window has a look similar to Figure 2.



Location, Height, Width, and Size

The Location property needs a Point to tell the TextBox where to start on the Form. The Size property decides how big the control is. You can also use the Width and Height properties instead of the Size property. Here's a code snippet that sets the Location, Width, and Height properties of a TextBox control:

myDynamicTextBox.Location = new Point(50, 50); // Sets the starting position
myDynamicTextBox.Width = 200; // Sets the width
myDynamicTextBox.Height = 30; // Sets the height

Multiline TextBox

By default, a TextBox control is set up to accept input in a single line. If you want it to handle multiple lines, you need to change the Multiline property to true. It's important to note that the Multiline property is initially set to false.

When you use the drag-and-drop method to add a TextBox control from the Toolbox to a Form, you might notice that you can't adjust the height of the TextBox control by default.



Certainly! However, if you choose a TextBox control, click on the Tasks handle, and mark the MultiLine CheckBox, you will notice that height resizing grip handles become accessible on the TextBox. This allows you to adjust the height of the control as needed.





You can achieve this dynamically by setting the Multiline property to true in your code. This way, you can enable the multiline feature for a TextBox programmatically. For example:

myDynamicTextBox.Multiline = true;

Background, Foreground, BorderStyle

The BackColor and ForeColor properties are employed to define the background and foreground colors of a TextBox, respectively. Clicking on these properties in the Properties window brings up the Color Dialog.

Alternatively, you can dynamically set the background and foreground colors at run-time. Here's a code snippet that does just that:


myDynamicTextBox.BackColor = Color.Yellow; // Sets the background color
myDynamicTextBox.ForeColor = Color.Blue;   // Sets the foreground color

This code dynamically adjusts the background and foreground colors of the TextBox during the program's execution. Adjust the Color values as needed for your design preferences.


You can customize the border style of a TextBox using the BorderStyle property. This property is represented by a BorderStyle enumeration with three values: FixedSingle, Fixed3D, and None. The default border style is Fixed3D. The code snippet below sets the border style of a TextBox to FixedSingle:

myDynamicTextBox.BorderStyle = BorderStyle.FixedSingle; // Sets the border style to FixedSingle

This code dynamically changes the border style of the TextBox during runtime. Adjust the BorderStyle property according to your visual preferences.



Name

The Name property signifies a unique identifier for a TextBox control, allowing access to the control in the code. The code snippet below demonstrates setting and retrieving the name and text of a TextBox control:

// Setting the name and text of the TextBox

myDynamicTextBox.Name = "myUniqueTextBox"; // Sets the unique name

myDynamicTextBox.Text = "Hello, TextBox!";  // Sets the text content


// Getting the name and text of the TextBox

string textBoxName = myDynamicTextBox.Name; // Gets the unique name

string textBoxText = myDynamicTextBox.Text; // Gets the text content


This code dynamically assigns a unique name and text to the TextBox, and later retrieves these values as needed in your code. Adjust the values as required for your application.

Text, TextAlign, and TextLength


The Text property of a TextBox holds the current text within the control. The TextAlign property determines text alignment, which can be Left, Center, or Right. The TextLength property provides the length of the TextBox contents.

Here's a code snippet demonstrating how to set the Text and TextAlign properties, and retrieve the size of a TextBox control:

// Setting the text and text alignment of the TextBox myDynamicTextBox.Text = "Aligned Text"; // Sets the text content myDynamicTextBox.TextAlign = HorizontalAlignment.Center; // Sets text alignment to Center // Getting the size of the TextBox int textBoxLength = myDynamicTextBox.TextLength; // Gets the length of the text content Size textBoxSize = myDynamicTextBox.Size; // Gets the size of the TextBox


This code dynamically configures the text and alignment of the TextBox and retrieves its length and size. Adjust the values based on your specific requirements.


Append Text
Indeed, one way to append text to a TextBox is by updating the Text property to include the current text along with the new text you want to add. Here's an example:



// Appending text to the TextBox myDynamicTextBox.Text = myDynamicTextBox.Text + " New Text to Append";


Clear, SelectAll and DeselectAll
The Clear method is used to remove the contents of a TextBox. Here's a code snippet that demonstrates using the Clear method:

// Clearing the contents of the TextBox
myDynamicTextBox.Clear();


The TextBox class provides the SelectAll and DeselectAll methods to respectively select and deselect all text within a TextBox control. Here's a code snippet illustrating how to use these methods:

// Selecting all text in the TextBox myDynamicTextBox.SelectAll(); // Deselecting all text in the TextBox myDynamicTextBox.DeselectAll();


Post a Comment

Thanks for comment.

Previous Post Next Post