View Single Post
  #1 (permalink)  
Old 09-12-2007, 05:51 AM
travy92's Avatar   
travy92 travy92 is offline
Learning Programmer
 
Join Date: Sep 2007
Location: Australia
Age: 15
Posts: 72
Credits: 14
Rep Power: 5
travy92 is on a distinguished road
Send a message via MSN to travy92
Post Tutorial - Picture Box with extra features!

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:

Quote:
Dim Error1 As Integer
Dim Pic As String
Now you can add this code:

Quote:
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
This is the full code, the explanation follows.

----------------------------------------------------------------

CODE EXPLANATION:

Quote:
Private Sub cmdLoad_Click()
Basically means whatever code is under this line gets executed.

----------------------------------------------------------------

Quote:
Pic = InputBox("What would you like to load?", "Yes?")
Makes an InputBox appear.

Remember: Type the picture location in the space given in the InputBox!


----------------------------------------------------------------

Quote:
On Error GoTo Error
This code tells VB to go to "Error" (Which is the title of the Error Handler section).

----------------------------------------------------------------

Quote:
Picture1.Picture = LoadPicture(Pic)
Simply means load the file that's location was specified by the user in InputBox.

----------------------------------------------------------------

Quote:
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 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!

----------------------------------------------------------------

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!:
Attached Files To view attachments in this forum your post count must be 1 or greater. You currently have 0 posts.
__________________
C:\Users\Travis\Desktop\Image Converter\Knight1.bmp

Last edited by Jordan; 09-13-2007 at 09:15 AM.
Reply With Quote

Sponsored Links