Beginning Objects in VB - Part 1

Tuesday Nov 19th 2002 by Sam Huggill
Share:

Beginning Objects in VB: Part 1

Hi there avid programmers! It's great to see you at the start of a new series of articles on objects and Visual Basic.

Well, lets start with the basics (excuse the pun): I'm Sam Huggill and I will be guiding you through the jungle of class modules, ActiveX components, DLLs and much more over the coming weeks. If you ever had any doubts about objects, read on!

In this first part, I will be covering the following things:

- What's an object?
- Where can I pick up an object?
- Why should I use objects?
- My first object!

OK - Now that we are clear about what I'm supposed to be doing, we better proceed! But just before we do, I want to remind you that I want to hear what you think about this tutorial. Boring, interesting, too much or too little? Let me know by clicking on the Post Feedback Now link at the bottom of each page of this tutorial.

Right, on with the learning!

Is it a bird? Is it a plane? No, it's an object! (Sorry for those of you who never watched superman :)

Objects are things that you can program with, things that make programming easier. They contain a set of methods and properties that allow you to make the object do certain things without actually having to look at the objects code. For example, you have probably used the following statement many times:

Text1.Text = ""

Remeber that? Well, what you are doing is setting the property Text, of the text box object Text1 to equal nothing.

See, objects are really easy, you've been using them ever since that first "Hello World!" program you wrote!

In fact, all the controls you see on your VB toolbox are all objects. For a better look at objects, either press F2 or click View, Object Browser. This nifty little window allows you to see all the properties and methods of the objects currently loaded.

If you click on the combo box that currently contains , and select VB, you will see a list (on the left) of all the standard controls in your toolbox.

Just try clicking on TextBox. See, recognise some of those methods and properties? (I sure hope so :)


Click here for a larger image.

Here's what we've done so far:

- Example of the Textbox object
- Realised how simple objects can be (yes, you have really :)
- Taken a look inside the object browser

Right, lets find out where you get other objects from >>

You've probably got loads on your desk, but don't bother looking if your filing system is anything like mine!

You can actually make your own objects in VB (as we'll discuss in a min), but for the moment you can load some of the more complex objects that come with VB. Here are a few simple steps to get you started:

- Open up VB, and select Standard EXE.
- Click Project, Components.
- Scroll down and select Microsoft RichTextBox Control 6.0 (or 5.0 if you're running VB5)

Your should have some thing that looks like this:

Righty ho, now click OK. The RichTextBox icon should be on your toolbox:

Go back to the object browser (F2 in case you forgot) and click on that combo box thingy again and select RichTextLib. Uhhh? What's all those things down the left hand side? This looks a bit more complicated then the Textbox.

Well, don't fret! I'm here to guide you through the murky depths of the RichTextBox control (Ooops, this isn't Shakespeare in VB).

Where were we? Oh yeah, scroll down and select RichTextBox (it has a little icon that looks like some Bassets AllSorts - ok, so it doesn't then :)

If you take a look on the right hand pane, then you will see a load of cool methods and properties. (If you have ever used this control before, then you will recognise some of them.)

So, there you have an easy way to find out about objects. But, (I here you cry), why should I use them? Lets find out....

Good question. Imagine this - every time you wanted to use a text box control, you had to add a usercontrol, copy and paste some code, draw a few controls on it, rename them all, test it and then load it onto your form! That would be ridiculous.

Lucky for us there is a way to get around this. We can make objects that contain all their own code and they only let you see a few important bits in the object browser.

This saves ages of time programming, and means that you can easily distribute your control (or whatever object it may be).

Although Visual Basic is not a completely object orientated language (that would have to include geeky things like inheritance and other complicated stuff) it does allow us to use objects in our code.

Now that you know why you should use them, and you've seen some examples of objects in VB (the Textbox and RichTextBox controls in case you had already forgotten), you may now be wanting to build your own (or you might not be wanting to build your own, but that's what we're going to do next so ha!)

So far we have discussed objects as controls in VB (the Textbox and RichTextBox - you should be remembering these by now!)

In this example, we're going to make a different kind of object. When you make a control, it has a user interface (that means you can visually see something on the screen, such as the textbox itself). Another kind of object is one that doesn't have a user interface, and we call this a DLL (of Dynamic Link Library if you want to impress your friends).

To make one of these, click File, New Project and select ActiveX DLL:

This makes a new project with one empty class module in it. On the properties pane, make sure that the Instancing property is set to 5 - MultiUse. This allows us to compile the DLL and expose the class module to other programs (such as the object browser (This is starting to get complicated! (Espically with all these brackets :)))

We only want this example to be simple, so why don't we base it around the cheesy Hello World project we talked about earlier on.

Lets add a new method called SayHello that pops up a message box saying Hello World! on it.

But before you rush off to start coding. let me show you a useful tool that VB has. It's called the VB Class Builder. To open it, click Project Add Class module and select VB Class Builder:

You'll probably get a message box telling you that the project contains a class module not generated by the Class Builder, but just ignore it and press ok.

We want to add a new method, so click on Class1 and select File, New, Method. Enter a name as SayHello and click OK. You should now see the item SayHello in the right hand pane (contact me if you don't, or simply scream :)

As that's all we want, click File, Exit and Yes to update the project.

As you will see, we didn't achieve much there! Two lines of procedure code - not much for all that faffying around. But as we continue through this series, you will get a handle on the uses of the Class Builder.

We only want a message box to show up, nothing too serious. Add this line of code:

Msgbox "Hello World!"

The procedure should look like this:

Public Sub SayHello()
    MsgBox "Hello World!"
End Sub

Time to build! Save the project, and a click File, Make Project1.DLL.

There you go! You've just complied your first object! (OK, so maybe this isn't your first time, but who cares?)

Now all we need to do is test it. Lets add another project - File, Add Projcet, Standard EXE. (Uhh? How do we load up the object - it doesn't appear under the components dialog box)

Good question (I dunno who asked it, but it was good :) Because a DLL is not a control, we need to make a reference to it (because we are not loading any kind of user interface)

So, click File, References and scroll down and select Project1:

(If it's not there, click on the browse button and locate the Project1.DLL)

Click ok and your object is now loaded. Just to make sure, press F2 to load up the object browser and select Projec1 from the combo box. See the SayHello method there? Good.

Now lets do some cool coding and call the SayHello method:

- Add a declaration of the object:

Dim m_clsProject1 As Class1

- Now, under the Form_Load event, create the object:

Set m_clsProject1 = New Class1

And call the SayHello method:

m_clsProject1.SayHello

Run the project and see the message box appear! (If you get an error let me know and I will sort it out :)

Wow! Is that the time? I better be on my way for now, but next week I'll come back to looking at what we have just done (creating objects and all that stuff) but for now, download the sample files:

Download the files!

And let me know what you thought of this article! (Remeber, click Post Feedback Now just below this text)

Have a nice week! (or was it Have a nice day? I'm losing my memory at the moment - more sleep required!)

C ya next week!
Sam

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved