SDLC
Difference between Process and Program
What is a Program? A program is an executable file which contains a certain set of instructions written...
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
The following are the most common properties of the Visual Basic TextBox control:
The purpose of events is to make the TextBox Control respond to user actions such as a click, a double click or change in text alignment. Here are the common events for the TextBox Control:
Step 1) To create a TextBox, drag the TextBox control from the toolbox into the WindowForm:
Step 2)
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
HelloTextBox.Text = "Hello. Welcome to gtupapers!"
End Sub
Step 4) You can now run the code by clicking the Start button located at the top bar:
Step 5) You should get the following form:
Here is a screenshot of the complete code for the above:
Explanation of code:
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 VB.net. 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:
Click the text box next to Username label, move to the Properties section located at the bottom left of the window. Give it the name UsernameField.
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
Now, run the code by clicking the Start button. A form will popup.
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:
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.
Click the text box control again and move the mouse cursor to the Properties section. Change the value of Multiline property to True.
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
GuruWelcome.Text = "Line 1"
GuruWelcome.Text = GuruWelcome.Text & ControlChars.NewLine & "Line 2"
End Sub
Upon execution, the two lines of text will be separated.
Explanation of Code:
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:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x As Integer
x = Integer.Parse(age.Text)
MessageBox.Show(x)
Run the code, and enter your age into the text box. Click the Show Age button. You should see the following:
The value you enter is returned in a MessageBox.
Explanation of Code:
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 gtupapers. Add the following code:
Private Sub gtupapers_TextChanged(sender As Object, e As EventArgs) Handles gtupapers.TextChanged
gtupapers.Text = "welcome to gtupapers"
gtupapers.ReadOnly = True
End Sub
The code should return the following:
Here is the code:
Explanation of Code:
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:
What is a Program? A program is an executable file which contains a certain set of instructions written...
Here are SCCM interview questions for fresher as well as experienced candidates to get their dream...
Torrent client is a software for downloading files that utilize a peer to peer system. This...
What are Decision Trees? Decision Trees are versatile Machine Learning algorithm that can perform...
What is a Data Frame? A data frame is a list of vectors which are of equal length. A matrix...
Document Object Model or DOM is an essential component of web development using HTML5 and...