Trending September 2023 # Vb.net Textbox Control Tutorial: Properties With Example # Suggested October 2023 # Top 13 Popular | Lifecanntwaitvn.com

Trending September 2023 # Vb.net Textbox Control Tutorial: Properties With Example # Suggested October 2023 # Top 13 Popular

You are reading the article Vb.net Textbox Control Tutorial: Properties With Example updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Vb.net Textbox Control Tutorial: Properties With Example

What is TextBox Control?

The TextBox Control allows you to enter text on your form during runtime. The default setting is that it will accept only one line of text, but you can modify it to accept multiple lines. You can even include scroll bars into your TextBox Control.

In this tutorial, you will learn

TextBox Properties

The following are the most common properties of the Visual Basic TextBox control:

TextAlign– for setting text alignment

ScrollBars– for adding scrollbars, both vertical and horizontal

Multiline– to set the TextBox Control to allow multiple lines

MaxLength– for specifying the maximum character number the TextBox Control will accept

Index– for specifying the index of control array

Enabled– for enabling the textbox control

Readonly– if set to true, you will be able to use the TextBox Control, if set to false, you won’t be able to use the TextBox Control.

SelectionStart– for setting or getting the starting point for the TextBox Control.

SelectionLength– for setting or getting the number of characters that have been selected in the TextBox Control.

SelectedText– returns the TextBox Control that is currently selected.

Textbox Events

AutoSizeChanged– Triggered by a change in the AutoSize property.

ReadOnlyChanged– Triggered by a change of the ReadOnly property value.

How to Create a TextBox

Step 1) To create a TextBox, drag the TextBox control from the toolbox into the WindowForm:

Step 2)

Move to the Properties section located on the bottom left of the screen. Change the name of the text box from TextBox1 to HelloTextBox:

Step 3) Add the following code to add text to the control:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Add text to the control chúng tôi = "Hello. Welcome to Guru99!" End Sub

Step 5) You should get the following form:

Here is a screenshot of the complete code for the above:

Explanation of code:

Creating a public class named Form1

Creating a sub procedure named Form1_Load. It will be called when the form is loaded.

End the subprocedure

End the class.

Password character

Sometimes, you want a text box to be used for typing a password. This means that whatever is typed into that text box to remain confidential. This is possible with chúng tôi It can be done using the PasswordChar property which allows us to use any character that you want. Let us demonstrate this using an example:

Begin by creating a new project. Drag two TextBox Controls, two Labels, and one Button into the form. Change the texts on the two labels and the button to the following:

Do the same for the TextBox Control next to Password label, giving it the name PasswordField.

You should now make the PasswordField TextBox Control show * for each character typed in it. Add the following code:

Private Sub PasswordField_TextChanged(sender As Object, e As EventArgs) Handles PasswordField.TextChanged PasswordField.PasswordChar = "*" End Sub

Type the username and the password and observe what happens. You should see the following:

The username is shown, but the password has been hidden. Here is the code for the above:

Explanation of code:

Creating a class named Form1.

Creating a sub-procedure named PasswordField_textchanged(). It will be called when the form is loaded.

Using the PasswordChar property to show * as a user types the password.

Ending the sub-procedure.

Ending the class.

Newline in TextBox

By default, you can only create one line of text in a text box. There are two ways through which we can achieve this. Let us discuss them.

Drag and drop a TextBox Control to your form. Give the control the name GuruWelcome.

Alternative, you can set the Multiline property to true in your code as follows:

GuruWelcome.Multiline = True

Add the following code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load GuruWelcome.Multiline = True chúng tôi = "Line 1" chúng tôi = chúng tôi & ControlChars.NewLine & "Line 2" End Sub

Upon execution, the two lines of text will be separated.

Explanation of Code:

Creating a class named Form1

Creating a sub-routine named Form1_Load()

Setting the Multiline property to True. The textbox will be able to take more than one lines.

Adding the first line of text to the text box.

Adding the second line of text to the text box. The Controlchars.NewLine property helps us to split the two lines.

Ending the sub-routine.

Ending the class.

Retrieving Integer Values

VB.net treats everything as a string. This means that you read an integer from the text box as a string, then you convert it into an integer. This is normally done using the Integer.Parse() method.

To demonstrate this, create a new text box control plus a button. Give the text box the name age. Give the button the name Button1. You should have the following interface:

Add the following code:

Dim x As Integer x = Integer.Parse(age.Text) MessageBox.Show(x)

The value you enter is returned in a MessageBox.

Explanation of Code:

Creating a class named Form1.

Defining an integer variable named x.

Converting the value read from the textbox named age into an integer.

Displaying the value converted in the above step in a MessageBox.

Ending the sub-procedure.

Ending the class.

ReadOnly TextBox

You can make a textbox readonly. This means that the user will not be able to modify the value displayed within the textbox. To do this, you set the ReadOnly property to True. To demonstrate this, create a new textbox and give it the name guru99. Add the following code:

Private Sub guru99_TextChanged(sender As Object, e As EventArgs) Handles guru99.TextChanged chúng tôi = "welcome to Guru99" guru99.ReadOnly = True End Sub

The code should return the following:

Here is the code:

Explanation of Code:

Creating a class named Form1.

Creating a subroutine named guru99_TextChanged.

Adding text to the textbox named guru99.

Setting the ReadOnly property of the textbox to True. This make the text on the textbox unmodifiable.

Ending the sub-procedure.

Ending the class.

max length

The MaxLength property can help you set the maximum number of words or characters that the textbox will allow. To demonstrate this, create a TextBox control and give it the name fullName. Add the following code:

Private Sub fullName_TextChanged(sender As Object, e As EventArgs) Handles fullName.TextChanged fullName.MaxLength = 8 End Sub

Run the code and try to type your full name. You will not be able to type more than 8 characters, with whitespace included:

The code:

Code Explanation:

Creating a class named Form1.

Creating a sub-procedure named fullName_TextChanged.

Making the fullName textbox accept a maximum of only 8 characters.

Ending the sub-procedure.

Ending the class.

Summary:

The TextBox Control allows you to enter text into your form during runtime. It is good for getting input from users.

The default setting is that the TextBox Control will only accept one line of text. However, it is possible for you to change this.

You can hide what the user types into the TextBox, especially when you need to capture passwords.

You can also set the maximum number of characters that you need to be entered into the TextBox.

You can make your TextBox un-editable, meaning that the users won’t be able to change the text displayed on it.

You're reading Vb.net Textbox Control Tutorial: Properties With Example

Update the detailed information about Vb.net Textbox Control Tutorial: Properties With Example on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!