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.



Add A TextBox Example
Add A TextBox Example

Adding a TextBox to a Form Using Code

To set up the form ready to add a text box follow these steps;

  1. Create a New Project called "Add a Coded Text Box"
  2. Drag a button from the Toolbox to the form and place at the bottom.
  3. 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

Add Text Box Code View
Add Text Box Code View

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.

The Code

Between the two lines of code circled write the following code.

Dim myTextBox As New TextBox
Me.Controls.Add(myTextBox)


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.

TextBox Control Code Example So Far
TextBox Control Code Example So Far

Setting The Text Box Text Align Properties

This part of the tutorial deals with the text align property, here we have 3 options,

  1. Left (default)
  2. Right
  3. Center

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

Have Your Say

Did You Find This Tutorial Helpful?

See results without voting

Comments 11 comments

stugod profile image

stugod 4 years ago from Bradford

I learnt to program in basic over 30 years ago. That was on the commodore. Still it was an extremely useful tool. I will now bore you to death with some bits of it. If,then,let,goto, (less than greater than). peek,poke,Alpha numeric and numeric strings. Lots more but I can never forget them;););)


GarethAdams2010 profile image

GarethAdams2010 4 years ago from United Kingdom Author

I did a little programming on the commodore myself, it was on the 64 version! Thanks for your comment I hope you like the hub!


stugod profile image

stugod 4 years ago from Bradford

Same here also on Gelma and luma industrial stuff. Automated kitchen manufacture. A world away from my normal job. Just fell into it by accident. Enjoyed it for a while. Very unhealthy put on three stone. Thanks..64 rocked. Got my first text to speech. I think they called it simons.


aisha91 profile image

aisha91 4 years ago from Manila, Philippines

Hi Gareth, seeing the textbox and way of coding above VB is quite similar to delphi. Delphi is really fun to use, to learn about and start some serious application with it. I am still learning this language and not yet knowledgeable, I want to learn VB and c# too, I think I'll be fascinated with those languages too as I am with delphi. Hope you'll share more knowledge about VB. :)


GarethAdams2010 profile image

GarethAdams2010 4 years ago from United Kingdom Author

Thanks for your comment, I will be adding more VB tutorials in the near future, the next hub I am working on is a password generator, this is a basic version of 1 I wrote a few years ago. Keep a look out for that within the next week or so! Also I will add some c# tutorials as well :)


aisha91 profile image

aisha91 4 years ago from Manila, Philippines

Wow, thank you...

I'll wait for your incoming hubs...:)


stugod profile image

stugod 4 years ago from Bradford

I have enough on my plate lately but I am following this. I expect like the old basic syntax is every thing in this topic. I would like to copy and paste the page if that is ok. So I can read a bit more in my pace.


velmurugan 4 years ago

Hi Friends.,

Any one can help that, How to find whether the particular control is present in form at run time in VB6. already i had a code for add/remove the controls in runtime but i need for find..


Pete 3 years ago

You need to post more articles. Straightforward and good explanation


JC 2 years ago

I was looking for a simple how to put the text in the box . . .

Turns out, it was as simple as textbox1.text = "put text here"


Johna981 2 years ago

Excellently written writeup, doubts all bloggers offered the same content material because you, the internet is actually a greater location. Please maintain it up! ecdeefkedgdd

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working