HomeBrainDump Making Your First Active X Control in Visu...
Making Your First Active X Control in Visual Basic
Visual Basic is, essentially, a way for programmers to save themselves a lot of time when creating applications for computers that run on Windows. Using Visual Basic makes a lot of sense when you want to build a no fuss, no muss application. This article will introduce you to using it.
Contributed by Katie Gatto Rating: / 7 July 09, 2008
If you are sitting here, reading this piece right now and staring at your screen, then you probably want to know how to add Active X controls to a Visual Basic program. After all, that is the title of this tutorial, and you are in the right place.
For those of you who don't know what Visual Basic is, Visual Basic is a way for the developers of programs to save time and energy when they are creating programs. Visual Basic, of course, is only for programs that are meant to run on versions of the Windows operating system (Vista, XP, ME, etc...), so you can not use it if you want to make a program that runs on the Apple or Linux operating systems.
Visual Basic helps to save time and repetition by creating the framework of the program for developers when they open the Visual Basic program. Visual Basic features what is known as object-oriented programming. In the case of Visual Basic this means that you, as the developer, will have access to a list of different items like text boxes, buttons, scroll bars, labels, etc. that you can place onto the main window of the program.
Now that you have a handle on Visual Basic, let's talk about Active X. If you have even seen some Active X code, then you probably know that developers can not make a whole program in Active X. This may raise some questions in your mind while you are reading this. Well, why not? Is there something wrong with Active X? Because if there is, why should you waste your time on a seriously flawed language?
No worries, there is nothing wrong with Active X. Active X does what it sets out to do very well; it is simply not designed to be the primary language for a program. Active X is more of a set of rules that act as a bridge between a program (for example a program written in Visual Basic) and a web browser (like Internet Explorer or Firefox). It acts as a kind of a bridge.
If Visual Basic was an island Active X would be the suspension bridge that lets the commuter data get across. Active X is one of those fancy multi-tiered bridges that reroutes the traffic to less crowded decks during rush hour. Sorry, the bridge analogy ran away with itself there; what I meant to say is that Active X can work with more than just Visual Basic, as those who know Active X can create bridges in programs like C++ and Java as well.
Now that we have gotten past the basics, it is time to dive in head first and make your first Active X control in a Visual Basic program. Don't worry, since I know that you are a first timer, we will take this nice and slow. Just one final note, this tutorial assumes that you have a least a basic working knowledge of Visual Basic as a program. If you don't, you may find yourself having a hard time with this tutorial.
This first step will be the easiest. So take that baby step in the right direction and open the Visual Basic program. You can do this by double clicking on the Visual Basic program icon, or by finding the program in the start menu. This will take you to a screen that asks you if you want to make a new program or open an existing one, which leads us to...
Step Two: Make a choice
If you are looking to add this brand new Active X control to a program that you are already working on, then open up that program, post haste. If, however you are just testing out your new skill (a practice run) then open a new project.
Step Three: Break down what you're making
Identify the kind of control that you will make, and what you want it to do. For the purposes of this example you are going to make a spinner box. In case you don't know what a spinner box is, picture a text box with numbers inside; next to it is a set of arrows, one is pointing up and the other is pointing down. When you click on an arrow the number in the box goes up or down. You have probably seen these before, and just not known the proper name.
Now that we know what we are making let's decide what elements we will need in order to make a spinner box. In this case we will need to have a text box and a vertical scroll bar.
Step Four: Make a container
Access the file menu in your open projects and add a new, blank Active X control. In some versions you can use the add project command option for this. Before you now should be a new blank grid for you to work with. This is the location where the real magic will happen; this is where we will make your new Active X control. This step is important because the Active X control needs a container to run properly.
On the left there is a list of possible items that you can use to make your control. In this case we are going to use two of the standard elements to make our spinner box. First select a text box from the options. Draw a small text box onto your workspace. Small is a critical aspect here; your text box only needs to be big enough to hold a number, so keep your sense of scale here.
Next, find the vertical scroll bar next to the text box. Remember, when we are finished with this, it will be one element, so these items need to be touching in order for the end product to look right.
Step Six: Merge the Controls
Use the control sizing handle to draw a box around both the text box and the vertical scroll box. This will allow us to make rules that will apply to both of the items, so be sure that the box encompasses both of the elements that we just added.
Step Seven: Display the code window
The code window will allow us to write the event driven code that will give the scroll bar its functionality. To do this all you have to do is double click on the vertical scroll bar. The code window will open automatically.
Step Eight: Add the code
In order to make the box functions work we need to add this line of code:
"text1.text=vscroll1.value"
This line of code makes the assumption that you kept the default names of the objects that you added to the program. If you made changes to names, then these lines of code will be slightly different. For example, if you re-named the text box texty1 then your text would read like this:
Close the code window, and the control form for the spinner. You can do this with the close box on the left hand side of these windows.
Step Ten: Draw the control onto the application
To do this, simply click on the tool box and then on your newly-minted control. Draw the control onto your main application.
Step Eleven: Test your application
Hit the "F5" key to test the application. Go ahead, give it a whirl; click on the up arrow, or the down as much as you want to, when you do this the number inside the box should go up and down when you click on the corresponding arrows.
There, now you've done it! Take a few seconds to congratulate yourself on learning a new skill. Go get a celebratory drink (non-alcoholic if you happen to be under age, at work or plan to drive later today) or send off a bragging email to one of your friends. Enjoy your new enhancement to your programming skill set; just be sure not to get an inflated ego until you have a few more under your belt.
One final note: this tutorial was made for Visual Basic 5.0; if you have a different version your controls and menus may have some slight differences and you will have to account for that. If you have a problem finding something you can always use the help menu to find it.