Wednesday, March 19, 2008

[VB6]Creating a MessageBox with VB6

Okay, to make a message box with VB6, I will assume you have VB6.


Now, open a new exe project.
You should see a window that says, Form 1.
Now, see those little dots on the form? Well now you should double click somewhere where those dots are.
You should now have a different screen, one that has two lines of code in it.
The lines should say,

Private Sub Form_Load()

End Sub


Now, add this line of code right after the Private Sub Form_Load():


Call MsgBox("Hello World!")


Now hit F5.
You should have a message box that pops up and says, Hello World!

Okay, so you have your message box, but then a blank form comes up, you're like, why the heck is this here?
Well, the code that you entered runs when the form loads, but not before the form shows up.
If you don't want the form to show up after you click, OK on the message box, then add this code right after the msgbox code:


End


Easy enough, right?
Well, now if you run it, (with F5) you will see that it shows the message, and then closes.

Okay, so you have that down, but say you want to be able to click a button and then have a message come up.
Well, that's easy too!
First, delete the MsgBox and the End code from your code view.
Then go back to the design view, (you can go there by clicking on the window that says, Form 1 (project))
Now, go over and click on the command button button.




Now click and drag on the design form, not let go and you should have a command button that says Command1.

Okay, now double click on the command button and you should have the code view again, except this time you will have this code:


Private Sub Command1_Click()

End Sub


Now put this line of code between the Private Sub Command1_Click() and End Sub:

Call MsgBox("Hello World!")


Now run the program, (F5) and you should see a form come up with a command button on it.
Click the button.
You should now see the message Hello World! come up.

Okay, so you get this, but how the **** do I get that dumb Project1 off of the top?!

Well, this is easy, and I will also teach you how to make button with a command button while I am at it.

Okay, so when you type in the call msgbox( then you get a big list of things that you don't understand. Well, I will give you a list of what they mean:


MsgBox(Prompt, [Buttons as VBMsgBoxStyle = VBOKOnly], [Title], [Helpfile], [context]) as VbMsgBoxResult


Okay, let's go thought it step by step:

MsgBox(Prompt


The Prompt is what shows up in the message box, you specify this with quotation marks. ("")


MsgBox(,[Buttons as VBMsgBoxStyle = VBOKOnly]


The buttons are what buttons show up if you specify this, the defualt is VBOk.
If you want a Yes and No button, then make a message box like this:

call msgbox("Hello World!", VbYesNo)


Then the message box will look this like this:
http://litzmanorbaptist.com/games/images/VBMsgBoxYN.bmp">


So you can now click on either of them and the message box just closes, but if you want to be able to see what they clicked, then make a code like this:


Dim YN

YN = msgbox("Hello World!", vbYesNo)
If YN = vbYes then
call msgbox("You clicked Yes!")
end if
If YN = vbNo then
call msgbox("You clicked no!")
end if



That is how you check to see if you have clicked Yes or No.


call msgbox( [Title]


The title is what shows up in the top, like Project1, except you can specify what it says, (if you don't specify anything, it defaults to what the project is called.)
This is what it would look like:




call msgbox("Hello World!", ,"Hello World! Application")


That would show a message box with Hello World! Application on the top.
Okay, I get it now, you say, but why the two commas?
Well, that space is to that it will skip the buttons, but if you wanted to but a button code in there you can.


call msgbox( [Helpfile], [context]


I don't really know what these do, but I assume that the help file is what it says. I have no idea what the context means.


Well, I hope this helped you to better learn how to use message boxes in VB6.

Now I am going to give my fingers a break, they kind of hurt. :p


~Elec0

No comments: