The TextBox Control
Text boxes should be used when you want the user to enter text that you have no knowledge of at design time (for example the name of the user). The primary function of a text box is for the user to enter text, but any characters can be entered, and it is quite possible to force the user to enter numeric values only.
First Design a form on choosing New Project of Windows Application. and then set the properties of label, button and forms like shown in above picture.
Adding the events
Code for Ok Button Click-
Code for Help Button Click-
Event on TextBox Keypress Event-
Text boxes should be used when you want the user to enter text that you have no knowledge of at design time (for example the name of the user). The primary function of a text box is for the user to enter text, but any characters can be entered, and it is quite possible to force the user to enter numeric values only.
Properties of TextBox
Name | Availability | Description |
CausesValidation | Read/Write | When a control that has this property set to true is about to receive focus, two events are fired: Validating and Validated. You can handle these events in order to validate data in the control that is losing focus. This may cause the control never to receive focus. The related events are discussed below. |
CharacterCasing | Read/Write | A value indicating if the TextBox changes the case of the text entered. The possible values are: q Lower: All text entered into the text box is converted lower case. q Normal: No changes are made to the text. q Upper: All text entered into the text box is converted to upper case. |
MaxLength | Read/Write | A value that specifies the maximum length in characters of any text, entered into the TextBox. Set this value to zero it the maximum limit is limited only by available memory. |
Multiline | Read/Write | Indicates if this is a Multiline control. A Multiline control is able to show multiple lines of text. |
PasswordChar | Read/Write | Specifies if a password character should replace the actual characters entered into a single line textbox. If the Multiline property is true then this has no effect. |
ReadOnly | Read/Write | A Boolean indicating if the text is read only. |
ScrollBars | Read/Write | Specifies if a multilane text box should display scrollbars. |
SelectedText | Read/Write | The text that is selected in the text box. |
SelectionLength | Read/Write | The number of characters selected in the text. If this value is set to be larger than the total number of characters in the text, it is reset by the control to be the total number of characters minus the value of SelectionStart. |
SelectionStart | Read/Write | The start of the selected text in a text box. |
WordWrap | Read/Write | Specifies if a multiline text box should automatically wrap words if a line exceeds the width of the control. |
Events of TextBox
Name | Description |
Enter GotFocus Leave Validating Validated LostFocus | These six events occur in the order they are listed here. They are known as "Focus Events" and are fired whenever a controls focus changes, with two exceptions. Validating and Validated are only fired if the control that receives focus has the CausesValidation property set to true. The reason why it's the receiving control that fires the event is that there are times where you do not want to validate the control, even if focus changes. An example of this is if the user clicks a Help button. |
KeyDown KeyPress KeyUp | These three events are known as "Key Events". They allow you to monitor and change what is entered into your controls. KeyDown and KeyUp receive the key code corresponding to the key that was pressed. This allows you to determine if special keys such as Shift or Control and F1 were pressed. KeyPress, on the otherhand, receives the character corresponding to a keyboard key. This means that the value for the letter "a" is not the same as the letter "A". It is useful if you want to exclude a range of characters, for example, only allowing numeric values to be entered. |
Change | Occurs whenever the text in the textbox is changed, no matter what the change. |
TextBox Test
First Design a form on choosing New Project of Windows Application. and then set the properties of label, button and forms like shown in above picture.
Adding the events
Code for Ok Button Click-
private void okbtn_Click(object sender, EventArgs e)
{
if (nametxt.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("Please fill all Information", "Alert", MessageBoxButtons.OKCancel,
MessageBoxIcon.Information);
}
else
{
string output;
// Concatenate the text values of the four TextBoxes
output = "Name: " + this.nametxt.Text + "\r\n";
output += "Address: " + this.textBox2.Text + "\r\n";
output += "Occupation: " + this.textBox3.Text + "\r\n";
output += "Age: " + this.textBox4.Text;
// Insert the new text
this.textBox5.Text = output;
}
} |
private void button1_Click(object sender, EventArgs e)
{
string output;
output = "Name = Enter Your name\r\n";
output += "Address = Enter Your address\r\n";
output += "Occupation = Enter Occupation\r\n";
output += "Age = Enter Your age";
// Insert the new text
this.textBox5.Text = output;
} |
Adding Events on TextBox-
Event on TextBox Leave Event-
private void nametxt_Leave(object sender, EventArgs e)
{
if (nametxt.Text == "")
{
nametxt.BackColor = Color.Red;
}
} |
private void nametxt_KeyPress(object sender, KeyPressEventArgs e)
{
if (nametxt.BackColor == Color.Red)
{
nametxt.BackColor = Color.Snow;
}
}
Using RichTextBox WinForms Control in C#.NET
ReplyDelete