VB6 Tutorial 32: Methods

By | 2018-05-15

This lesson is about some important and common methods in Visual Basic 6. There is a large number of built-in methods in VB6. This section describes the methods that you’ll use most frequently in your program.

What is a method?

A method is a built-in function that works with an object. A method has no meaning without its object. But this is very important to note that a method is not a special thing in VB6 but this is just a function, a block of code, which is associated with an object. The method causes the object to perform a specific task. See the following examples:

Example

Form1.Hide

The Hide method hides the form Form1 without unloading from memory. So a method performs an action for a particular object.

Example

'In Form1 
Cls                 
Text1.SetFocus

The Cls method clears the object, generally the form and PictureBox object. In the above code, the method Cls is written without any object. But I said before that it has no meaning without an object. This is because, in this example, Cls means Form1.Cls.

When you’re coding in the form module, you can omit the form name while invoking a form’s method.

In the above code, SetFocus is a method that moves the focus to TextBox. Here also the method causes the object to do something. There are so many methods in Visual Basic.

So now have a look at some of the methods discussed below.

Print

The Print method prints something on the object, generally on form and PictureBox.

Example

Form1.Print "Your text here"

Or

Print "Your text here"
Picture1.print "Your text here"

Here Picture1 is the PictureBox control.

Move

The Move method moves an object.

Syntax

Object.Move Left, [Top], [Width], [Height]

Note: The parameters enclosed in square brackets “[ ]” are always optional. Do not type the brackets in your code.

Example

Form1.Move 20, 50, 2000, 2000

Example Project

In the attached example, I have built a project that not only demonstrates the Move method, but also takes advantage of some of the other things that we have learned in previous lessons, such as:

While this example may seem like a little too much to take in, the idea is for you to see how the things we have gone over so far tie together. Don’t get discouraged!

Challenge!

And you thought there were no quizzes in this tutorial. Try a few things with the example project:

  • What happens if you change the value of lngIncrement?
  • There is purposely two variables defined and set, but not used. Can you spot them?
  • Try adding code to change the text inside the textboxes to reflect what the box is doing (growing, moving),
    • or, the x/y coordinates in the moving textboxes
    • or, the height/width values in the growing textbox

ZOrder

When vb controls are overlapped, the ZOrder method sets the position of the control. You can position the control in front of or behind other controls. To position the control in front of other controls, simply write the method without any argument. To position it behind other controls, pass 1 as an argument.

Example

Text1.ZOrder 1  'moves it behind other controls
Text1.ZOrder     'moves it to the front

Cls

The Cls method clears the form or PictureBox. But it does not mean that it will clear anything on the form or PictureBox. The Cls method clears the contents that are created or drawn by the graphic methods such as Print, Circle etc.

Example

Print "hello"
Picture1.Print "welcome"
Picture1.Cls
Form1.Cls            'or Cls

Here Picture1 is the PictureBox. This program will not print anything as Cls method is executed after printing a text.

Show

The Show method is generally used with multiple forms. It shows an object, a form.

Example

Form2.Show

Refresh

The Refresh method repaints or redraws an object completely. You can immediately update the content of a control using the Refresh method.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.