Introduction
A textBox is a crucial component of Windows forms. The user can enter data into the application using TextBox, which can be a single line or many lines. Actually, the class name is TextBox, and its definition is present under the namespace System.Windows.Forms. In C#, TextBox can be created by two different methods:
- Design-Time
- Run-Time
Let us see these two methods in detail with implementation.
Design-Time
This one is the easiest way to make a RichTextBox.
Following are the steps to do it.
- The first step will be to create a form window, as shown in the following image:
- In Visual Studio, you will have to click on the File menu, then go to New. After that, click on Project, then WindowsFormApp.
- We will drag and drop the TextBox Control available in the toolbox to the form in the second step.
The TextBox can be placed anywhere on the form as per requirements.
- After successful drag and drop, the properties of the TextBox control can be modified easily as per your requirements.
Run-Time
In this method, you can create a TextBox control using a program with the help of syntax provided by the TextBox class.
Following are the steps to create a TextBox control dynamically;
The TextBox class provides the TextBox() constructor for creating a TextBox control.
- The first step is to create a textbox with the help of the TextBox() constructor.
// Creating textbox
TextBox Mytextbox = new TextBox();
- After successfully creating the RichBox control, in the second step, we have to set the property as per our requirement of the TextBox control provided by the TextBox class.
// Set location of the textbox
Mytextbox.Location = new Point(187, 51);
// Set background color of the textbox
Mytextbox.BackColor = Color.LightGray;
// Set the foreground color of the textbox
Mytextbox.ForeColor = Color.DarkOliveGreen;
// Set the size of the textbox
Mytextbox.AutoSize = true;
// Set the name of the textbox
Mytextbox.Name = "text_box1";
- The last step is to add the created RichTextBox control to the form. This can be done by:
// Add this textbox to form
this.Controls.Add(Mytextbox);
Let us see an example:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace my {
public partial class MyForm: Form {
public MyForm() {
InitializeComponent();
}
private void MyForm_Load(object sender, EventArgs e)
{
// Let's create Lable1 and setting its properties
Label Mylablel = new Label();
Mylablel.Location = new Point(96, 54);
Mylablel.Text = "Enter Name";
Mylablel.AutoSize = true;
Mylablel.BackColor = Color.LightGray;
// Add this label to form
this.Controls.Add(Mylablel);
// Creating and setting the properties of TextBox1
TextBox Mytextbox = new TextBox();
Mytextbox.Location = new Point(187, 51);
Mytextbox.BackColor = Color.LightGray;
Mytextbox.ForeColor = Color.DarkOliveGreen;
Mytextbox.AutoSize = true;
Mytextbox.Name = "text_box1";
// Add this textbox to form
this.Controls.Add(Mytextbox);
}
}
}
Output:
Properties of TextBox
Some of the essential properties are:
- AutoSize: It is used to adjust the size of the TextBox according to the content.
- BorderStyle: It is used to adjust the border type of the textbox.
- BackColor: It is used to set the color of the background of the TextBox.
- Events: It is used to provide a list of event handlers that are attached to this Component.
- Location: It is used to modify the coordinates of the upper-left corner of the textbox relative to the upper-left corner of its form.
- Margin: It is used to set the margin between two textbox controls.
- Visible: It is used to get or set a value that determines whether the control and all its child controls are displayed.
Recommended topics, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable
Frequently Asked Questions
What is TextBox control?
Text box controls are used for entering text at runtime on a form. A single line of text is taken in by default. But, you can change the settings to accept multiple texts. Also, we can even add scroll bars to it.
What is the difference between TextBox and RichTextBox?
The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox will enable you to select the Font and other formatting properties for selections within the text displayed in control.
What are the properties of TextBox control?
TextBox Properties
- TextAlign– for setting text alignment.
- ScrollBars– for adding scrollbars, both vertical and horizontal.
- Multiline– to set the TextBox Control to allow multiple lines.
- MaxLength– for specifying the maximum character number the TextBox Control will accept.
- Index– for specifying the index of the control array.
What is the difference between text and label?
A Text is like a geometric shape (like a Rectangle or a Circle), while the label is a control based on UI (like a Button or a CheckBox). In Swing, geometric shapes were restricted to the painting mechanism, while in JavaFX, they can be used in more generic ways.
What is TextBox control with an example?
A TextBox control is used to accept or display, as input, a single line of text. VB.Net programmers use TextBox control extensively to let the user view or enter a large amount of text. A text box object is generally used to display text on a form or to get user input while a VB.Net program is running.