INTRO!
Hello everyone! Today we'll be making a program that loads a picture (pretty simple huh?) BUT it will have extended features! (Error handling AND InputBox)
----------------------------------------------------------------
GUI (LAYOUT):
Here is my GUI:
----------------------------------------------------------------
WHAT YOU NEED:
1 Command Button with:
Name: cmdLoad
Caption: Load
1 Picture Box:
Name: Picture1
Caption: (None)
----------------------------------------------------------------
CODE:
Add this code in General Declarations before you add the other code:
Now you can add this code:Dim Error1 As Integer
Dim Pic As String
This is the full code, the explanation follows.Private Sub cmdLoad_Click()
Pic = InputBox("What would you like to load?", "Yes?")
On Error GoTo Error
Picture1.Picture = LoadPicture(Pic)
Error:
Select Case Err.Number
Case 53
Error1 = MsgBox("Sorry, File not found! Please try again!", _
vbCritical + vbOKOnly, _
"File not found!")
Case Else
MsgBox (Err.Number & ", " & Err.Description)
End Select
End Sub
----------------------------------------------------------------
CODE EXPLANATION:
Basically means whatever code is under this line gets executed.Private Sub cmdLoad_Click()
----------------------------------------------------------------
Makes an InputBox appear.Pic = InputBox("What would you like to load?", "Yes?")
Remember: Type the picture location in the space given in the InputBox!
----------------------------------------------------------------
This code tells VB to go to "Error" (Which is the title of the Error Handler section).On Error GoTo Error
----------------------------------------------------------------
Simply means load the file that's location was specified by the user in InputBox.Picture1.Picture = LoadPicture(Pic)
----------------------------------------------------------------
This code is titled by "Error" which is the part where VB is supposed to go to if an error occured with the file location. Basically waht it does it when VB encounters an error, this part swings into action and gives you a message saying the file wasn't found OR another one with a different message (Case Else). This advantages of this is the fact that it wont close the program if it encounters an error! Disadvantages are that it requires extra coding LOL!Error:
Select Case Err.Number
Case 53
Error1 = MsgBox("Sorry, File not found! Please try again!", _
vbCritical + vbOKOnly, _
"File not found!")
Case Else
MsgBox (Err.Number & ", " & Err.Description)
End Select
End Sub
----------------------------------------------------------------
This is goodbye for now!
Thanks to TheComputerMaster and Tcm9669 for the ScreenShot program.
Special thanks to you for reading this tutorial.
Made by me, Travy92!
P.S - If you need help with any of the tutorials i've posted then feel free to PM me or post in the threads related to it!
SAMPLES LOL!:
Last edited by Jordan; 09-13-2007 at 06:15 AM.
Well instead of the Inputbox, I would have used the component called 'Microsoft Common Dialog Control' as it makes it simpler to browse for the picture. But this shows the basic concept so it is good! And it seems like you have got the 'Case' concept! That is very, very good.
Yeah, the case concept is very simple now thanks to you !
EDIT - I can't use "Microsoft Common Dialog Controls" because when i try to add the item from the toolbox (After ticking "Microsoft Common Dialog Controls") i get an error saying i don't have the license to do it...!
Last edited by travy92; 09-13-2007 at 03:46 AM.
nice post... keep it up..
How can i save those things as exe ?
The explanation has reallly helped, the input box is really useful for me in my program
@Travy: Maybe you have a trial version or something?
thanks a lot
very helpful post!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks