Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   TextBox Control in C# (http://www.go4expert.com/articles/textbox-control-c-sharp-t3293/)

Sanskruti 7Mar2007 19:37

TextBox Control in C#
Windows Forms text boxes are used to get input from the user or to display text. The TextBox control is generally used for editable text, although it can also be made read-only. Text boxes can display multiple lines, wrap text to the size of the control, and add basic formatting. The TextBox control allows a single format for text displayed or entered in the control.

To display multiple types of formatted text, use the RichTextBox control.The text displayed by the control is contained in the Text property. By default, you can enter up to 2048 characters in a text box. If you set the MultiLine property to true, you can enter up to 32 KB of text.

The code below sets text in the control at run time.

The InitializeMyControl procedure will not execute automatically it must be called.
Code: CPP

private void InitializeMyControl()
   // Put some text into the control first.
   textBox1.Text = "This is a TextBox control.";

Insertion point in a TextBox control

When a Windows Forms TextBox control first receives the focus, the default insertion within the text box is to the left of any existing text. The user can move the insertion point with the keyboard or the mouse. If the text box loses and then regains the focus, the insertion point will be wherever the user last placed it.

To control the insertion point in a TextBox control

1. Set the SelectionStart property to an appropriate value. Zero places the insertion point immediately to the left of the first character.

2. Set the SelectionLength property to the length of the text you want to select.
Code: CPP

private void textBox1_Enter(Object sender, System.EventArgs e)
   textBox1.SelectionStart = 0;
   textBox1.SelectionLength = 0;

The TextBox insertion point is visible by default in a new form only if the TextBox control is first in the tab order. Otherwise, the insertion point appears only if you give the TextBox the focus with either the keyboard or the mouse.

To make the text box insertion point visible by default on a new form Set the TextBox control's TabIndex property to 0.

A password box is a Windows Forms text box that displays placeholder characters while a user types a string.

To create a password text box

We have to Set the PasswordChar property of the TextBox control to a specific character.
The PasswordChar property specifies the character displayed in the text box. For example, if you want asterisks displayed in the password box, specify * for the PasswordChar property in the Properties window.

Set the MaxLength property. The property determines how many characters can be typed in the text box. If the maximum length is exceeded, the system emits a beep and the text box does not accept any more characters. Note that you may not wish to do this as the maximum length of a password may be of use to hackers who are trying to guess the password.

The code below initializes a text box that will accept a string up to 14 characters long and display asterisks in place of the string. Using the PasswordChar property on a text box can help ensure that other people will not be able to determine a user's password if they observe the user entering it.
Code: CPP

private void InitializeMyControl()
   // Set to no text.
   textBox1.Text = "";
   // The password character is an asterisk.
   textBox1.PasswordChar = '*';
   // The control will allow no more than 14 characters.
   textBox1.MaxLength = 14;

Multiple lines in the TextBox control

By default, the Windows Forms TextBox control displays a single line of text and does not display scroll bars. If the text is longer than the available space, only part of the text is visible. You can change this default behavior by setting the MultiLine, WordWrap, and ScrollBars properties to appropriate values.

To view multiple lines in the TextBox control

1. Set the MultiLine property to true. If WordWrap is true then the text in the control will appear as one or more paragraphs; otherwise it will appear as a list, in which some lines may be clipped at the edge of the control.

2. Set the ScrollBars property to an appropriate value.

None:Use this value if the text will be a paragraph that almost always fits the control. The user can use the mouse pointer to move around inside the control if the text is too long to display all at once.

Horizontal: Use this value if you want to display a list of lines, some of which may be longer than the width of the TextBox control.

Both:Use this value if the list may be longer than the height of the control.

3. Set the WordWrap property to an appropriate value.

fasttiger111 4Mar2010 17:27

Re: TextBox Control in C#
how to show output in a text box. the output is an integer

satyedra pal 19Apr2010 16:31

Re: TextBox Control in C#
For showing output in the TextBox we can use following:

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

public class addnum : Form {

    Button Bsum  = new Button();

    TextBox textBox1 = new TextBox();
    TextBox textBox2 = new TextBox();
    TextBox textBox3 = new TextBox();


    public addnum() {
        Bsum.Location = new Point(40, 40);
        Bsum.Text = "sum";

        Bsum.Click += new System.EventHandler(this.bsum_Click);


        textBox1.Location = new Point(15, 25);
        textBox1.Size = new Size(80, 10);
        textBox1.Text = "1int";

        textBox2.Location = new Point(15, 70);
        textBox2.Size = new Size(80, 10);
        textBox2.Text = "2int";

        textBox3.Location = new Point(15, 170);
        textBox3.Size = new Size(80, 10);
        textBox3.Text = "outputint";


    private void bsum_Click(object sender, System.EventArgs e) {
        int a = Convert.ToInt32(textBox1.Text);
        int b = Convert.ToInt32(textBox2.Text);
        int c = a + b;
        textBox3.Text = c.ToString();

    public static void Main(string[] args) {
        Application.Run(new addnum());

shahad 30Apr2010 18:30

Re: TextBox Control in C#
how can i take input from the same textbox again again when i need

satyedra pal 3May2010 11:57

Re: TextBox Control in C#
you can take the value of TextBox again and again by using the TextBox.Text keyword ,it can be TextBox1.Text or Text?Box2.Text or........... so on.It will find out the value of TextBox as input.

kien_vn 6Sep2010 12:42

Re: TextBox Control in C#
Length attribute of the Textbox Control does not work well when the Mutilline attribute is set

snoop911 9Oct2010 11:42

Re: TextBox Control in C#
I'm very new to C# so I'm not sure if the TextBox is the best control for my situation...

I would like to have a column of 30 unique text fields. Programmatically I will initialize the text within each field, but I'd like the user to be able to change any of them as well.

Would an array of these textboxes be the best way to access this data from the code, or is there a better approach? My only concern is that 30 text boxes may look a bit ugly compared to an excel type look.

The only other toolboxes I see that may be applicable are DataGridView and DataSet.. but not sure if that would be the wrong way to go about it.

Any suggestions?


T4Family 13Oct2010 22:33

Re: TextBox Control in C#

Don't know if you still need this, but I am understanding that your concern could also be settled through using a ListBox form.

DataGridViews are superior to ListBoxes, but also harder to manage. I too am newer to the Windows Forms C# programming, and i am trying to wrap my mind around all of this. ListBoxes are cool, but DataGridViews are very powerful, and once i understand more of programming for them, I will definitely prefer them over other ways.

shamilahamed 25Jan2011 00:22

Re: TextBox Control in C#
hello friends,
i am developing a application in c#. i am new to c#.
i need a "Code to pass textbox values to gridview table without using database."

Kujuna 16May2011 22:08

Re: TextBox Control in C#
It's so useful. Thank you a lot.

donor 13Jan2012 11:34

Re: TextBox Control in C#
private void InitializeMyControl()
// Set to no text.
textBox1.Text = "";
// The password character is an asterisk.
textBox1.PasswordChar = '*';
// The control will allow no more than 14 characters.
textBox1.MaxLength = 14;

To create a password textbox

All times are GMT +5.5. The time now is 10:30.