Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

C# TextBox Control



A TextBox control is used to display or accept as input, a single line of text. This control has additional functionality that is not found in the standard Windows text box control, including multiline editing and password character masking.

A text box object is used to display text on a form or to get user input while a C# program is running. In a text box, a user can type data or paste it into the control from the clipboard.

For displaying a text in a TextBox control, you can code like this.

textBox1.Text = "https://getsuyodev.com";

You can also collect the input value from a TextBox control to a variable like this way.

string var;
var = textBox1.Text;

C# TextBox Properties

You can set TextBox properties through Property window or the program. You can open Properties window by pressing F4 or right-click on a control and select Properties menu item.

The below code set a textbox width as 250 and height as 50 through source code.

textBox1.Width = 250;
textBox1.Height = 50;

Background Color and Foreground Color

You can set background colour and foreground colour through property window and programmatically.

textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;

Textbox BorderStyle

You can set 3 different types of border style for the textbox; they are None, FixedSingle and fixed3d.

textBox1.BorderStyle = BorderStyle.Fixed3D;

TextBox Events

keydown event

You can capture which key is pressed by the user using KeyDown event

e.g.

TextChanged Event

When user input or setting the Text property to a new value raises the TextChanged event

e.g.

Textbox Maximum Length

Sets the maximum number of characters or words the user can input into the text box control.

Textbox ReadOnly

When a program wants to prevent a user from changing the text that appears in a text box, the program can set the controls Read-only property is to True.

Multiline TextBox

You can use the Multiline and ScrollBars properties to enable multiple lines of text to be displayed or entered.

Textbox password character

TextBox controls can also be used to accept passwords and other sensitive information. You can use the PasswordChar property to mask characters entered in a single
line version of the control

The above code set the PasswordChar to *, so when the user enters the password, then it displays only * instead of typed characters.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Width = 250;
            textBox1.Height = 50;
            textBox1.Multiline = true;
            textBox1.BackColor = Color.Blue;
            textBox1.ForeColor = Color.White;
            textBox1.BorderStyle = BorderStyle.Fixed3D;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string var;
            var = textBox1.Text;
            MessageBox.Show(var);
        }
    }
}

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More