Understanding C# TextBox Controls: A Guide for Developers

In Windows Forms, TextBox plays an important role. With TextBox, users can enter data in the application, which can be single or multi-line. TextBox is a class that defines the System.Windows.Forms namespace below. In C#, you can create a TextBox in two different ways:

1. Design time: This is the easiest way to create a TextBox, as shown in the following steps:

Step 1:

Create a Windows Form. As shown in the figure below:

Visual Studio > File > New > Project > WindowsFormApp

Step 2:

Drag and drop the text box control from the toolbox onto the Windows Forms. You can place the TextBox anywhere on Windows Forms as needed.

Step 3:

After dragging and dropping, you’ll go to the properties of the TextBox control to modify the TextBox design as needed.

2. Runtime: It’s a bit trickier than the method above. In this approach, you can use the TextBox class to create your own text box.

Step 1: Create a text box using the TextBox() constructor provided by the TextBox class. create a text box TextBox Mytextbox = new TextBox();

Step 2: After creating the TextBox, set the properties of the TextBox provided by the TextBox class. Set the position of the Mytextbox text box. Location = new Point(187, 51); Set the background color of the text boxMytextbox.BackColor = Color.LightGray; set the text box Mytextbox.ForeColor = Color.DarkOliveGreen; foreground; Set the size of the text box Mytextbox.AutoSize = true; Set the name of the text box Mytextbox.Name=”text_box1″;

Step 3:

Finally add this text box control to use

Add() method

.

//Add this textbox to form
this.Controls.Add(Mytextbox);

Example:

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 my {
  
public partial class Form1 : Form {
  
     public Form1()
     {
         InitializeComponent();
     }
  
     private void Form1_Load( object sender, EventArgs e)
     {
  
         //Creating and setting the properties of Lable1
         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);
     }
}
}

The output is as follows:

Important properties of TextBox

AcceptsReturnThis property is used to set a value that is displayed in a multi-line TextBox control whether pressing the ENTER key in the control is to create a new line of text in the control or to activate the default button for a given form.
AutoSizeThis property is used to resize the TextBox based on the contents.
BackColorThis property is used to set the background color of the text box.
BorderStyleThis property is used to adjust the border type of the text box.
CharacterCasingThis property is used to check if the TextBox control has modified the case of the characters when typing.
EventsThis property is used to provide a list of event handlers attached to this component.
FontThis property is used to adjust the font of the text displayed by the text box control.
ForeColorThis property is used to adjust the foreground color of the text box control.
LocationThis property is used to adjust the coordinates of the upper-left corner of a textbox control relative to the upper-left corner of its form.
MarginThis property is used to set the margin between two textbox controls.
MaxLengthThis property sets the maximum number of characters that a user can type or paste into a text box control.
MultilineThis property is used to set a value that shows whether this is a multi-line TextBox control.
NameThis property is used to give the TextBox control a name.
PasswordCharThis property is used to set the characters used to mask the password characters in a single-line TextBox control.
ScrollBarsThis property is used to set which scrollbars should appear in a multi-line TextBox control.
TextThis property is used to set the text associated with this control.
TextAlignThis property is used to adjust the alignment of the Chinese text of the TextBox control.
TextLengthThis property is used to get the length of the Chinese text of the TextBox control.
UseSystemPasswordCharThis property is used to set a value that shows whether the text in the TextBox control should be displayed as the default password character.
VisibleThis property is used to obtain or set a value that determines whether the control and all of its children are displayed.