PDA

Click to See Complete Forum and Search --> : Tutorial - Beginners Guide To VB6


Madboy
Mar 6th, 2004, 03:51 PM
Author:
Madboy

Description:
Tutorial

Requirments:
Visual Basic 6

Below is a few simple methods of the basics to using Visual Basic 6. Please note that the information may be wrong, or different to your methods. But this tutorial shows the basics, and gives the user an understanding to using Visual Basic.

Getting Started

The most important thing with any program, especially programs with more features is to familiarise yourself with its interface. You should look around the program at all its options it has to offer you, and customise it to suit your needs and preferences. Spending time to get to know your program is essential, and will pay off in the future:)

Naming things to remember them easily

When you drag controls on your form, they are instantly named based on its control, followed by a number representing the current amount of its control kind on the current form. For example adding a command button will be named Command1, yet adding another will be named Command2. This is not good practice, as you will find when adding multiple controls. The way to do it is by using name conventions (or suffixes). Here is a small list example:


Command Button = cmd
Form = frm
Label = lbl
Picture Box = pic
Textbox = txt


Normally if i have only say one Textbox on a form, i would name it txtMain. But with command buttons etc i would name them based on their caption, or action. For example a command button called Exit would be named cmdExit. Why is this better you ask? Because identifying a control named say cmdEnter is better and easier to find then say Command57!

Important Notes

Always use Option Explicit at the top of your code windows, this is to ensure you are using the correctly named variables and that non are mixed up.
You can also make Visual Basic add this automatically to every new code window. Just go to:
Tools > Options > Check "Require Variable Decleration"

Never use END to close your program. Although you can no longer see your program, the memory it has used is stored into the computer's RAM still, which will make the users computer very sluggish :( The work around is to use Unload Me (you can replace the Me with the form name too). Alternatively you can use Set Me = Nothing (Personally i use Unload Me).
Of course you dont want to repeat code over and over, so from your Main Form, you could add this code, to Unload All Forms From Memory:

Always use the Tab key to space between statements, such as If, Else and Then. It makes it easier to read, thus easier to change later on.

Always use comments, dont worry when you compile your program, the comments are removed and are not added to your final program size ;)

Option Explicit

Private Sub Form_Unload()
Dim frm As Form
For Each frm In Form
Unload frm
Next frm
End Sub

Message Box

To create a simple message you could do the following:

MsgBox ("This is my message")

Then you can simply add a comma (,) at the end of the closed bracket to be introduced with inital message box options, such as icons etc.

Message Box - [Advanced]

This example shows how to store a response in a variable, so you can answer to a question.

Private Sub Form_Load()
Dim Response As Integer

Response = MsgBox("Exit Program?", vbInformation + vbYesNo, "Title")

If Response = vbYes Then
Unload Me
ElseIf Response = vbNo Then
Me.Refresh
End If

End Sub

It also shows how to add Yes and No to the message box, as well as an icon and Message title.

Menus
If you dont know what a menu is, then dont learn programming :p
From VB you can easily create menus, however they can only be set at design time. To create them, you must first open the Menu Editor, you can do so by clicking the Menu Editor button from the toolbar or by pressing Ctrl+E. Now, click on the Caption field, and type your Menu bar caption. This example, will show how to create a Edit menu, like the one seen in Notepad. So in the Caption field type &Edit The & means Ampersand, and underlines the following character. Now from the list below, you will see &Edit, before adding a sub item we must name the Menu Item. We will use mnuEdit. Now from the list below, click Insert, and use the arrows if necassary to position the sub item. To let VB know this is a sub item use the Right arrow once.(IE this item goes directly underneath our main menu). Now we add the Undo sub item. So for the caption we add &Undo, and name it mnuEditUndo. Now create a new sub item in the same way. Now we will make a seperator (this is the line that appears in the menu). we use a - (hyphon) for the caption, and i would name it mnuEditSeperator1. Now finish the menu off as you wish, the layout should look like this:


&Edit
---&Undo
----
---&Cut
---&Copy
---&Paste
---&Delete
----
---Select &All

Close the Menu Editor and look on your form, is the menu there?

Menus - Creating a Popup Menu

To create a popup menu, create a menu like before from the Menu Editor, but make the first Item Visible = False. (you should name it mnuPopup too).

This code will create a popup when the form is right clicked:

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Popup mnuPopup
End If
End Sub

Its as simple as that ;)

Modules?

What is a module you ask? A module is used to store all your functions and public routines seperately. Apart from it keeping your code editors window clean, it removes the need for repetitive typing. For example, it is better to add code to a module and use 1 line to call it when needed, then to repeat the same code over and over. This code from the module Exits the program when needed:

Public Sub ExitProgram()
Unload frmMain
End Sub

It has to be named Public so VB knows it is available to all forms. Now, from the form, this code is called when you click either the File > Exit menu item or the Exit Command button, like so (This code goes in your form's code window):

Private Sub mnuFileExit_Click()
Call ExitProgram
End Sub

Private Sub cmdExit_Click()
Call ExitProgram
End Sub

Although there is no significant size difference in using the code form the module, as there is using it in your form's code, it saves time and space later on when dealing with larger code!

Components and Libraries

A component (ActiveX *.ocx) is a control used in your VB IDE environment. The basic components consist of labels, frames, textboxes etc. You can add new components by going to the Project > Components menu.

Q) When i download a ActiveX component from a third-party web site it doesnt show in the components menu. How do i fix this?

A) ActiveX controls need to be registered to your system. To do this, first its reccomended copying the *.ocx file into the C:\WINDOWS\SYSTEM directory. Then goto (from Windows)
Start > Run > regsvr32 "C:\WINDOWS\SYSTEM\MyControl.ocx"
Then you may need to restart VB for the changes to take affect.

This applies the same for DLL's (Dynamic Link Libraries).

To unregister the control or Dll goto:
Start > Run > regsvr32 /u "C:\WINDOWS\SYSTEM\MyControl.ocx"

I hope this simple guide to VB helped all you Noobs ;)

vbNeo
Mar 7th, 2004, 04:41 PM
You might want to add that the MsgBox only has one required value, the other values are Optional, that's why they are in brackets in VBs yellow quick info([optional]).

You might also consider providing more info in your tutorial - the users you are writing this to might now know anything about VB, and they'll want everything explained.

Otherwise, nice guide... =)

Madboy
Mar 7th, 2004, 04:52 PM
Originally posted by vbNeo
You might also consider providing more info in your tutorial - the users you are writing this to might now know anything about VB, and they'll want everything explained.

Otherwise, nice guide... =)

Thanks, its constantly been edited. Perhaps i shouldnt of posted it yet:p

si_the_geek
Mar 22nd, 2004, 12:11 PM
The code/files within this thread (submitted: 3-6-2004) have been checked for malware by a moderator.

Disclaimer: This does not necessarily mean that any compiled files (DLL/EXE/OCX etc) are completely safe, but any supplied code does not contain any obvious malware. It also does not imply that code is error free, or that it performs exactly as described.

It is recommended that you manually check any code before running it, and/or use an automated tool such as Source Search by Minnow (available here (http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=22222&lngWId=1) or here (http://sourcesearch2.homestead.com/)).
If you find any serious issues (ie: the code causes damage or some sort), please contact a moderator of this forum.

Usage of any code/software posted on this forum is at your own risk.