Getting Started with Visual Basic 2008
This tutorial is geared to teach individuals who are brand new to Visual Basic 2008 and want to create their first program in the language. The tutorial will cover proper naming styles, transferring information to a label, creating the code to execute the program on a button click, creating shortcuts, commenting, managing the tab order, and exiting an application.
Contents [hide]
- Creating the Form
- Creating Shortcuts
- Conventional Naming
- Exiting the Application
- Recording and Displaying the Data
- Clear the text boxes
- Tab Order
- Attachments
Visual Basic (VB) is a very powerful language, that makes creating programs quick and simple. VB has a GUI interface that allows the user to drag and drop tools that they want to use, such as a textbox, or label. A major advantage is that this saves a lot of time, in C++ or other languages you would need to hard code a date-time picker, yet VB provides that for you with customizable options and presents it in a pleasant way. Another great feature is that VB recognizes where you have placed other items and can automatically align your next item with previous ones.
VB isn't just drag and drop, to really make a great program though you're going to have to write your own code, and VB seamlessly switches between the two views.
Let's get started!
Creating the Form
Go to File > New Project and select Windows Form Application
(make sure that you have Visual Basic selected in the upper left)

Name your program, select a location to save it and click OK.
You should now have "Form1" and a VB layout similar to mine.

I am going to make a program that will record prices, dates, and descriptions of items I buy at a garage sale and sell on eBay.
Note: You do not need to copy my program verbatim, you should come away with an understanding of the basics of VB, but I will be building upon this setup for future tutorials showcasing more advanced aspects of programming in VB.
The fields of information I wish to record are: the date I bought the item, how much I paid for it, a description of the item, the date I sold it, how much I sold it for, my shipping cost, the cost I charged to ship it, and an ebay listing fee.
Now that I know what I want in my form, I can start working on the layout. Start your layout now.
Here is how mine looks, take a look at it, and then I will explain why I chose to lay it out the way I did.

Starting at the top of the image, you'll notice that I have changed the name of my form which is a property available in the Properties menu (off to the right). The next arrow points to a textbox, which is available in the Toolbox (to the left). Also available to us via the Toolbox are the date time pickers, labels, and buttons - if you're having trouble locating these items in the Toolbox, they are sorted in alphabetical order and are located under Common Controls. To change the label's text from Label1, change the Text property under appearance to a descriptive label - the same applies for the buttons. The last arrow points to a shortcut that I have included to allow me to jump to the eBay Listing Fee textbox.
Creating Shortcuts
To create a shortcut, select your label, and in the label name place and ampersand (&) before the letter you want the shortcut to be assigned to. For example, the "B" in eBay Listing Fee is the shortcut so I have e&Bay Listing Fee as my text property. To quickly jump to the textbox once my program is running all I need to do is hold Alt + B - this is another useful feature VB makes simple to implement.
I recommend exploring properties for the tools we are using here. Notice that you can change the font properties (size, font, style) along with the border style of the textbox and the back color as well which all helps add a nice touch to your program.
While viewing my current layout, you may notice that it is evenly spaced, and aligned. VB allows me to change the location (as well as size) of each item on the form so I spaced each item equally.
Conventional Naming
Now, we aren't done quite yet with the layout, but we are close and will begin coding the back-end shortly. What we need to do now is create labels for the entered information to be stored in, and give proper design names to the labels and textboxes.
The VB naming conventions can be viewed here on Microsoft's website. The ones we will be using are dtp for DateTime Picker, lbl for Label, txt for Textbox, and btn for Button. I have a text box for the item description, so the name for this would be: txtItemDescription - there are no spaces allowed, and we need to start the name with a letter. Likewise, the label for item description would be lblItemDescription.
I have given all my labels and textboxes their proper names. To hide the labels I will be storing the entered data in, I have cleared the text fields and they will not be visible to the user until a value has been entered in them. When selecting all of my program (Ctrl + A) I see this:

A problem you may encounter through erasing the text in the field is that you can't select the label. To select the label, make a mouse selection around the area that it is located.
Exiting the Application
Now for the coding! The first part I am going to code, is the Exit button because it requires the least amount of work. Double click the Exit button and VB will open the code view for you, with some code already in there.
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End Sub
End Class
In the Private sub, type Application.Exit()
You may have noticed VB's auto-complete feature which can be quite useful when you aren't 100% sure which features are available to an item or function. In this case, Exit() is a function already written for us by the guys at Microsoft and we just put that code in the button's sub. If you run your program (F5) and hit the exit button (or Alt + x) it will close the application.
This is what you should have:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
End Class
While this does what we want, it is not complete because there is no commenting to explain what this Sub does. Application.Exit() is self-explanatory, but you should get into the habit of commenting your Sub's. To explain what a sub does, I like to do block commenting which you can view in the next code section. In VB you may comment using an apostrophe (') and writing your comment after. If you want to comment out many lines you can select the text, and click the comment button.

To undo the comments, select the text and hit the Uncomment button located to the right of the comment button.
Recording and Displaying the Data
Alright, now we're going to code the Record button, so go back to design mode (through the tab at the top) and double click the Record button. It will then place the code for that button underneath the Exit button code.
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'****************************************************************
'* Ends the program '****************************************************************
Application.Exit()
End Sub
Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
End Sub
End Class
This upcoming part will be the meat of the program, but don't worry it's simple!
We want to send the data entered into the text boxes to the appropriate labels. With the labels and text boxes properly named, this step will be quick to complete thanks to the auto-complete feature. Your text boxes and labels should be similar in names. To send the data from the text box to the label we will set the label equal to the text box. -If we instead set the text box equal to the label we would lose the data entered into the text box.
'****************************************************************
'*Record text boxes to lables, then clear the text boxes
'****************************************************************
lblDateAcquired.Text = dtpDateAcquired.Text
lblPricePaid.Text = txtPricePaid.Text
lblDateSold.Text = dtpDateSold.Text
lblItemDescription.Text = txtItemDescription.Text
lblSellingPrice.Text = txtSellingPrice.Text
lblShippingPrice.Text = txtShippingPrice.Text
lblShippingCost.Text = txtShippingCost.Text
lbleBayFee.Text = txteBayFee.Text
End Sub
Clear the text boxes
If we wanted to enter multiple items, we would not want to have to clear the data fields each time, so we can set the program to do it for us by setting the values equal to nothing. Enter this code below "lbleBayFee.Text = txteBayFee.Text", but above "End Sub".
txtPricePaid.Text = ""
txtItemDescription.Text = ""
txtSellingPrice.Text = ""
txtShippingPrice.Text = ""
txtShippingCost.Text = ""
txteBayFee.Text = ""
A common error in logic can occur when implementing a feature such as this one. For instance, if you were to put this code above all of your values you will end up with blank labels, because you have already cleared the information from the text boxes.
Tab Order
Tab order is very simple to manage. While in Design View (viewing the layout, not the code) go to View > Tab Order. Here is mine:

Create a logical order for the program to tab through by clicking in the order you want the user to tab through. The only boxes that matter while doing this, are the ones that the user puts information in, the labels containing text do not need to be touched. Press Esc when you're done editing the tab order.
Congratulations on finishing the tutorial. Here is an image of the programming running after entering an item, as well as my final code.

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'****************************************************************
'* Ends the program '****************************************************************
Application.Exit()
End Sub
Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
'****************************************************************
'*Record text boxes to lables, then clear the text boxes
'****************************************************************
lblDateAcquired.Text = dtpDateAcquired.Text
lblPricePaid.Text = txtPricePaid.Text
lblDateSold.Text = dtpDateSold.Text
lblItemDescription.Text = txtItemDescription.Text
lblSellingPrice.Text = txtSellingPrice.Text
lblShippingPrice.Text = txtShippingPrice.Text
lblShippingCost.Text = txtShippingCost.Text
lbleBayFee.Text = txteBayFee.Text
'* Clear text boxes
txtPricePaid.Text = ""
txtItemDescription.Text = ""
txtSellingPrice.Text = ""
txtShippingPrice.Text = ""
txtShippingCost.Text = ""
txteBayFee.Text = ""
End Sub
End Class
If you need any additional help, please post in our forums.
Omee (not verified)
Wow very detailed tutorial,
Wow very detailed tutorial, thank you for this. Always wanted to figure out how events and forms work in VB! This explains it very easily!
Retweeted!
Colin (not verified)
Wow this was really simple to
Wow this was really simple to create! I like how simple Visual Studio makes creating a GUI. Looking forward to more in the series.
Baran Ornarli
Great tutorial Dan, really
Great tutorial Dan, really well described in great detail.
Sahara (not verified)
Ah I never thought of tab
Ah I never thought of tab orders...
Anonymous (not verified)
hey nice tutorial, u got
hey nice tutorial, u got anymore tutorials on this ebay programme you made? thanks
Anma (not verified)
How simple and explanatory.
How simple and explanatory. great
Anonymous (not verified)
thanks..this is great
thanks..this is great
Post new comment