Visual Basic ... A Basic tutorial (EX1)
Results 1 to 2 of 2

Thread: Visual Basic ... A Basic tutorial (EX1)

  1. #1
    Junior Member
    Join Date
    Sep 2002
    Posts
    10

    Visual Basic ... A Basic tutorial (EX1)

    This isnt really a tutorial more of a few VERY SIMPLE exercises which will help someone learn the basics of Visual Basic. I know that many people dont like Visual Basic and some say its childish but I feel that it helps to have an Idea as so much is written in VB these days.

    I havnt ever written a tutorial befor so please dont flame me much (Im very sensitive)

    I Apologise in advance for my bad english. I not the best of writters.

    -----------------------------------------------------------------------------------------

    EXERCISE 1

    AIM: Create a simple program with a button which when pressed displays a message on the form and pop up an alert box.

    1) Create a folder in the place you wish to save this new VB project, e.g c:\Push Me

    2) Start VB6 and select standard exe

    3) On the form draw a Command Button by selecting the option off the toolbar on the left hand side of the screen

    4) On the form draw a Label by selecting the option off the toolbar on the left hand side of the screen

    5) Now you have your form sorted you need to set properties in order for them to do what you want. To set properties select the item you wish to change and then in the bottom right hand corner there should be a window in which the properties are set out.

    Set the following properties to your objects

    Command Button:

    Name = cmdPushMe
    Caption = Push Me
    FontSize = 14

    Label:

    Name = lblDisplay
    Caption = (leave blank)

    Form:

    Name = frmFirst


    6) Now you have your form and properties sorted you need to add the code for your project. To add coding for the button double click on it. A box should pop up in which you should insert the following code...

    Sub cmdPushMe_Click()

    lblDisplay.Caption = "ouch"

    MsgBox "Not That Hard!"

    End Sub

    7) Now run your program by clicking the play button in the top toolbar (just like the play button on your VCR)

    8) Go to File - Save Project As and save the project to the folder you created in step 1)

    --------------------------------------------------------------------

    Exercise 2 : Hello Project

    Remember : Each New Project Should Be Saved In A New Folder

    1) Create a new folder in which to save this project, e.g c:\Hello

    2) On a new form, draw one label and three command buttons and set the following properties:



    Command Button1:

    Name = cmdDisplay
    Caption = Display

    CommandButton2:

    Name = cmdClear
    Caption = Clear
    Enabled = False

    Command Button3:

    Name = cmdExit
    Caption = Exit

    Label:

    Name = lblMessage
    Caption = (leave Blank)
    Font Size = 14

    Form:

    Name = frmGreetings
    Caption = Greetings!


    2) There are three events possible here:

    - the cmdDisplay button is clicked
    - the cmdClear button is clicked
    - the cmdExit button is clicked

    so you must attach code to each button.

    When the Display button is clicked we want to:

    Display 'Hello Pat' in the label, lblMessage
    Make the clear button enabled (so user can click it)
    Make the Display button disabled (so user cant click it)


    so, attach this coding to the display button:

    Sub cmdDisplay_Click()

    lblMessage.Caption = "Hello Pat"
    cmdClear.Enabled = True
    cmdDisplay.Enabled = False

    End Sub


    3) Attach similar coding to the clear button, so that the following will happen when it is clicked:

    Display nothing in the label, lblMessage
    Make the Display button enabled (So user can click it)
    Make the clear button disabled (So user cant click it)


    Work this out for yourself :-P

    HINT: code it between

    Sub cmdClear_Click()

    End Sub


    4) Attach code to the exit button so that the program will end when this button is clicked:

    Sub cmdExit_Click()

    End

    End Sub


    5) Run your program and see what it does

    6) Use save project as to save the project to the folder you created in step 1

    ------------------------------------------------------------------

    EXERCISE 3 : Simple Maths Project

    Create a project with an interface like the attached gif "ex3"

    Add the necessary coding to add, subtract and multiply the numbers entered and then to display the result of the calculation in txtResult.

    The clear button should clear txtNo1, txtNo2 and txtResult ready for the user to input two new numbers.

    The Exit button should end the program ... duh!

    Hints For Exercise 3


    Labels have a caption property, but text boxes have a Text property

    Some simple code attached to the add button could be:

    [B]Sub cmdAdd.Click()

    txtResult.Text = Val(txtNo1.Text) + Val(txtNo2.Text)

    End Sub

    Val is needed here because we are using numbers

    Remember to do the same steps as in exercise 1 & 2:

    - Create a new folder for this project
    - Draw the interface
    - Set the properties of each control on the interface
    - Attach COde to each button onn the interface
    - Test the program
    - Save the project using save project as

  2. #2
    Junior Member
    Join Date
    Sep 2002
    Posts
    10
    Exercise 4 : Improving the simple maths project

    Practise some new techniques, by modyfying the maths project as follows:

    1)

    Add some comments at the start of your coding. For example

    'Bob Marley
    '
    'October 2002
    '
    'Copyright By Me
    '==============
    '

    2)

    Change the font size and font colour of the text in txtResult.

    Do this by setting the properties of txtResult in the design view of the form

    3)

    Make the clear button also return the cursor to the txtNo1 text box.

    This is called 'setting the focus'. Add in the line of code

    txtNo1.SetFocus

    4)

    Make the clear button disabled when the program first starts

    Do this by setting the enabled property in the design view of the form

    5)

    Make the clear button so that it is only enabled after a result has been calculated, and so that it becomes disabled again once the three text boxes have been cleared.

    Use, eg. cmdClear.Enabled = True

    6)

    Make the add / subtract / multiply buttons disabled after one of them has been clicked and enabled again after clear has been clicked

    7)

    Make the exit button so that it displays a MsgBox to say "Goodbye" befor the program ends.

    8)

    Change the background colour of the Exit button

    Hints
    - Do this in the design view of the form
    - First set the style property to graphical


    9)

    Experiment with other properties and effects.

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides