TextBox Control in C# with Examples – Programming, Pseudocode Example, C# Programming Example
C# Windows Form

TextBox Control in C# with Examples

In this tutorial, you will learn about essential control in the WinForms application world, which is the TextBox Control.

A TextBox is widely used in Windows Application, mainly to get or set data from/to a data source. Just like other controls, a TextBox has its properties and events. this tutorial will teach you about some of the common properties of a TextBox Control, as well as the events that every WinForms Developer should know.

The TextBox is a class and it is defined under System.Windows.Forms namespace. In C#, you can create a TextBox in two different ways:

1. Design-Time: It is the simplest way to create a TextBox as shown in the following steps:

  • Step 1: Create a windows form. As shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the TextBox control from the ToolBox and drop it on the windows form. You can place TextBox anywhere on the windows form according to your need.
  • Step 3: After drag and drop you will go to the properties of the TextBox control to modify the TextBox design according to your requirement.

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create your own textbox using the TextBox class.

TextBox Control Properties

Below are some of the most used properties for the TextBox Control:

Name – Name or unique identifier of the TextBox. For better coding practice, name a TextBox starting with the prefix ‘txt’. Example: txtFirstName

Multiline – A boolean property which is used to determine whether a TextBox is a single or multiline TextBox. The default value is false.

PasswordChar – To set the desired char for a password. For example:

Will change the TextBox into a Password field:

ReadOnly – to change the TextBox to ReadOnly if needed. It’s a boolean property that has a default value of false. Example:

Text – To set the text of a TextBox Control. Example:

Visible – A boolean property to show/hide the control on the form. The default value of true. Example:

Will hide the control on the form.

Font – To set the font name, size, bold, etc. of the TextBox.

Important properties of TextBox

PropertyDescription
AcceptsReturnThis property is used to set a value which shows whether pressing ENTER in a multiline TextBox control creates a new line of text in the control or activates the default button for the given form.
AutoSizeThis property is used to adjust the size of the TextBox according to the content.
BackColorThis property is used to set the background color of the TextBox.
BorderStyleThis property is used to adjust the border type of the textbox.
CharacterCasingThis property is used to check whether the TextBox control modifies the case of characters as they are typed.
EventsThis property is used to provide a list of event handlers that are attached to this Component.
FontThis property is used to adjust the font of the text displayed by the textbox control.
ForeColorThis property is used to adjust the foreground color of the textbox control.
LocationThis property is used to adjust the coordinates of the upper-left corner of the 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 is used to set the maximum number of characters the user can type or paste into the text box control.
MultilineThis property is used to set a value which shows whether this is a multiline TextBox control.
NameThis property is used to provide a name to the TextBox control.
PasswordCharThis property is used to set the character used to mask characters of a password in a single-line TextBox control.
ScrollBarsThis property is used to set which scroll bars should appear in a multiline TextBox control.
TextThis property is used to set the text associated with this control.
TextAlignThis property is used to adjust the alignment of the text in the TextBox control.
TextLengthThis property is used to get the length of the text in the TextBox control.
UseSystemPasswordCharThis property is used to set a value which shows whether the text in the TextBox control should appear as the default password character.
VisibleThis property is used to get or set a value which determine whether the control and all its child controls are displayed.

2 Comments

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.