VB TextBox Example - Advanced Features of The Visual Basic Text Box
The Visual Basic Text Box
Visual basic text boxes are one of the more common controls used when programming, they are useful for data input, passwords, returning data, and data evaluation. With such a wide range of uses I have put together a few tutorials using some of the more common uses of the Visual Basic Text box control.
Note: These tutorials were written using Visual Basic 2010 Express Edition
If you are using older versions these tutorials should still work, if you have any issues you can search for details on your favourite search engine.
Adding a Text Box control to a Windows Form
In Visual studio you can simply add a text box from the toolbox (usually located on the right hand side of the IDE from the default settings. The other way to add a textbox to the form is using code, in the example below we will add a text box to the form using code.
Adding a TextBox to a Form Using Code
To set up the form ready to add a text box follow these steps;
- Create a New Project called "Add a Coded Text Box"
- Drag a button from the Toolbox to the form and place at the bottom.
- Double click the button to get in to the code view.
- Set the Text property of the Button to "Add a Text Box"
- Set the Name property of the button to addTextBoxButton
Your form should look like the one on the right and your code view should look like the one below on the left:
Note: If you are new to programming it is wise to name controls like I do to help you follow along
Writing The Code To Add a Text Box
After you have double clicked on the button you will be taken to what is known as the "code view", this is where you write the code that will add a text box to the form, look at the image on the right and you will see the initial code page you get when you first enter. You will notice that there are a few lines of code visible that were generated by Visual Studio, here we are only really concerned with the Button Event Handler (the code circled), this is where we will write code so that if we click on the button we will see a new text box appear on the Form.
Between the two lines of code circled write the following code.
Dim myTextBox As New TextBox
Now run your application and click on the button, you will see a nice new textbox added to your form! See below for a quick explanation before moving on.
How The Code Works
So all we really did was create a new instance of a textbox to work with, we called this myTextBox, then we told the visual compiler to add the instance of the textbox to the form, in visual basic we always work with an "instance" of an object, we always create an instance using the NEW keyword. This method works with pretty much any control, remember however that we have not positioned the textbox yet we just added to the default location! Positioning the textbox will come in the next tutorial.
Positioning The Text Box on The Form
In this short example we are going to set where we want the new textbox to be on the form, to do this add the following line of code under the 2 that you added in the last section.
myTextBox.SetBounds(92, 32, 150, 0)
So the above line of code simply sets the following properties of the text box
- x position (92 in the example above)
- y position (32 in the example)
- width of the control (150 in the example)
- height of the control (0 in the example (see below))
ok so the x position is how far in from the left you want the control, you can change the value in the example above to see where it ends up and to give you an idea of the values your changing. The y position is how far down the form you want to add the control, again play around with the figures to see the affect, the width is simply how wide you want your control.
The height of the control in this example is not required as a text box and many other controls have a set height, so even though we set the value to 0 it uses the default single line height, the same applies if we had set it to 1000 simple because we are using the text box in "single line". If we were using the text box in "multi line" mode we would be able to set the height (explained next)
The Text Box Control Multi line Property
In this next line of code we add we are going to set the multi line (or multiline) as it is known in code to "true" this tells the compiler that you want to be able to write on various lines of your text box instead of the default single line. To accomplish this we add the following line of code
myTextBox.Multiline = True
Now if you return to your setbounds value and change the height to 100 you will see that the textbox now grows in height, leaving the value as 0 will appear as if the textbox isn't there as it has no value! See the image below for the code so far if you are following along.
Setting The Text Box Text Align Properties
This part of the tutorial deals with the text align property, here we have 3 options,
- Left (default)
To set the property we simply access the "TextAlign" property of myTextBox, the following 3 lines of code deal with each of the 3 possible settings,
myTextBox.TextAlign = HorizontalAlignment.Left
myTextBox.TextAlign = HorizontalAlignment.Center
myTextBox.TextAlign = HorizontalAlignment.Right
Choose the one you require for your application and paste it in, if you have read this far you may see a pattern showing, this pattern is simply that we use our instance of the textbox control, then the . notation and then select a property, to find out more properties you can access you can simply add a control to the form from the toolbox then look at the properties in the left hand window!
More Text Box Control Properties
Here we are going to run through some of the most common properties of the TextBox control, these are fairly simple to understand but feel free to ask questions below!
Tab Index (important if you are creating an input form to allow users to tab from box to box in order)
myTextBox.TabIndex = 1
Use System Password Characters (great if you want to make a hidden field where the characters show as *)
myTextBox.UseSystemPasswordChar = True
Read Only (useful if you only want to display non editable text to the user)
myTextBox.ReadOnly = True
Back Color (changes the back color of the TextBox)
myTextBox.BackColor = Color.Red
ForeColor (changes the color of the Text in the TextBox)
myTextBox.ForeColor = Color.Wheat