Net-informations.com
SiteMap  | About    

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.

C# textbox

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 = "http://csharp.net-informations.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 through program. You can open Properties window by pressing F4 or right click on a control and select Properties menu item

Textbox property window

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 color and foreground color 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 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.

Textbox keydown event

TextChanged Event

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

e.g.

Textbox textchanged event

Textbox Maximum Length

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

textBox1.MaxLength = 40;

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.

textBox1.ReadOnly = true;

Multiline TextBox

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

textBox1.Multiline = true;

Textbox passowrd 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

textBox1.PasswordChar = '*';

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

How to Newline in a TextBox

You can add new line in a textbox using many ways.

textBox1.Text += "your text" + "\r\n";

or

textBox1.Text += "your text" + Environment.NewLine;

How to retrieve integer values from textbox ?

int i; i = int.Parse (textBox1.Text);

Parse method Converts the string representation of a number to its integer equivalent.

String to Float conversion

float i; i = float.Parse (textBox1.Text);

String to Double conversion

double i; i = float.Parse (textBox1.Text);

How to allow only numbers in a textbox

Many of us have faced a situation where we want the user to enter a number in a TextBox. Click the following link that are going to make a Numeric Textbox which will accept only numeric values; if there are any values except numeric. More about.... How do I make a textbox that only accepts numbers

Autocomplete TextBox

Autocomplete TextBox in C#

From the version Visual Studio 2005, some of the controls support Autocomplete feature including the TextBox controls. The properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data source. More about.... C# Autocomplete TextBox

From the following C# source code you can see some important property settings to a TextBox control.




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);
        }
    }
}




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.