Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > C#

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

TextBox Control in C#


On 7th March, 2007
Unhappy TextBox Control in C#

Show Printable Version Email this Page Subscription Add to Favorites Copy TextBox Control in C# link

Author

Sanskruti ( Ambitious contributor )

Yet to provide details about himself


All articles By Sanskruti

Recent Articles

Similar Articles

Introduction



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.
Old 03-04-2010, 04:27 PM   #2
Newbie Member
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
fasttiger111 is on a distinguished road

Re: TextBox Control in C#


how to show output in a text box. the output is an integer
fasttiger111 is offline   Reply With Quote
Old 04-19-2010, 04:31 PM   #3
Contributor
 
Join Date: Mar 2010
Posts: 93
Thanks: 0
Thanked 3 Times in 3 Posts
Rep Power: 1
satyedra pal is on a distinguished road

Re: TextBox Control in C#


For showing output in the TextBox we can use following:
Code:
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);

        this.Controls.Add(bsum);

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

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

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

        
    }

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

Last edited by shabbir; 04-19-2010 at 06:50 PM. Reason: Code blocks
satyedra pal is offline   Reply With Quote
Old 04-30-2010, 06:30 PM   #4
Newbie Member
 
Join Date: Apr 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
shahad is on a distinguished road

Re: TextBox Control in C#


how can i take input from the same textbox again again when i need
shahad is offline   Reply With Quote
Old 05-03-2010, 11:57 AM   #5
Contributor
 
Join Date: Mar 2010
Posts: 93
Thanks: 0
Thanked 3 Times in 3 Posts
Rep Power: 1
satyedra pal is on a distinguished road

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.
__________________
satyedra pal is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
100 Multiple choice questions in C coderzone C-C++ 115 09-02-2010 03:29 AM
C# interview questions Sanskruti C# 11 08-18-2010 04:58 PM
ANSI C Standard Amit Ray C-C++ 1 03-06-2008 12:38 PM
How to use COM Interop in C# Nataraj1978 C# 1 02-20-2006 08:49 PM

 

All times are GMT +5.5. The time now is 05:28 AM.