Results 1 to 4 of 4

Thread: Tutorial - Beginners Guide To VB6

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Tutorial - Beginners Guide To VB6

    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

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Unload()
    4. Dim frm As Form
    5.     For Each frm In Form
    6.         Unload frm
    7.     Next frm
    8. End Sub

    Message Box

    To create a simple message you could do the following:

    VB Code:
    1. 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.

    VB Code:
    1. Private Sub Form_Load()
    2. Dim Response As Integer
    3.  
    4. Response = MsgBox("Exit Program?", vbInformation + vbYesNo, "Title")
    5.    
    6.     If Response = vbYes Then
    7.         Unload Me
    8.     ElseIf Response = vbNo Then
    9.         Me.Refresh
    10.     End If
    11.    
    12. 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
    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:

    Code:
    &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:

    VB Code:
    1. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     If Button = vbRightButton Then
    3.         Popup mnuPopup
    4.     End If
    5. 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:

    VB Code:
    1. Public Sub ExitProgram()
    2. Unload frmMain
    3. 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):

    VB Code:
    1. Private Sub mnuFileExit_Click()
    2. Call ExitProgram
    3. End Sub
    4.  
    5. Private Sub cmdExit_Click()
    6. Call ExitProgram
    7. 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
    Last edited by Madboy; Mar 22nd, 2004 at 12:24 PM.

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994
    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... =)
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929
    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 or here).
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width