How do i load a file with the common dialog controls (never learned how)
Printable View
How do i load a file with the common dialog controls (never learned how)
Try that:
Code:Private Sub Command1_Click()
On Error GoTo handling:
Dim cd1 As CommonDialog
Set cd1 = CommonDialog1
cd1.DialogTitle = "Open a File" 'Title of the CD
cd1.Filter = "Applications|*.exe|Text Files|*.txt" 'The filters for the files
cd1.FilterIndex = 1 'Choose the index of the filters
cd1.CancelError = True 'Make it so that it will not process if nothing selected
cd1.ShowOpen
'Put your file process stuff here
'
'
'
'
handling: 'a file was not selected
End Sub
thanks, only 7 mnutes to respond!!!!!!!! thanks da_silvey
uh, how do i use this?
add a common dialog box then add this code to command button 1
or do you need help with something else?
yeah how do I make it load a pic, there is nothing that specifies the filename or what was chosen
sorry about that, firstly, you probably want your users to load the pic, not the prograM??
if you want the program to pick the pic without any user intervention, you do not need a common dialog, but if you want the user to be able to choose the picture you can use it.
the filterindex determines what is shown in the dialog box,
the filename is held, after the user fives the ok, in the cd1.filename variable
Code:Private Sub Command1_Click()
On Error GoTo handling:
Dim cd1 As CommonDialog
Set cd1 = CommonDialog1
cd1.DialogTitle = "Open a File" 'Title of the CD
'Change this bit below to "JPEGS|*.jpg" for pictures
cd1.Filter = "Applications|*.exe|Text Files|*.txt" 'The filters for the files
cd1.FilterIndex = 1 'Choose the index of the filters
cd1.CancelError = True 'Make it so that it will not process if nothing selected
cd1.ShowOpen
'Put your file process stuff here
'
'
'
'
handling: 'a file was not selected
End Sub
do you get it?
Or if you need to load a picture into a picturebox (from what i could understand) then you would use:Code:'Add a command button and the control to a form and add this code.
Private Sub Command1_Click()
'Brings up the OPEN dialog
Dim iFile As Integer
On Error GoTo User_Cancelled
With CommonDialog1
.CancelError = True
.DialogTitle = "Please select a file..."
.Filter = "File (*.*)|*.*"
.ShowOpen
'Runs the selected program
Shell CommonDialog1.FileName, vbNormalFocus
End With
User_Cancelled:
End Sub
Hope that helps,Code:Private Sub Command1_Click()
'Brings up the OPEN dialog
Dim iFile As Integer
On Error GoTo User_Cancelled
With CommonDialog1
.CancelError = True
.DialogTitle = "Please select a file..."
.Filter = "File *.bmp|*.bmp|File *.jpg|*.jpg|File *.gif|*.gif"
.ShowOpen
'Sets the selected image as the background of the form
'just replace the form1.picture with whatever you need.
Form1.Picture = LoadPicture(CommonDialog1.FileTitle)
End With
User_Cancelled:
End Sub
D!m
Hello Dim,
I tried your code and it crashes at:
".CancelError = True"
and says "Object Required"
I don't know what's wrong but I tested the code on a new project and it works but when I incorporate it into my program it crashes on the line above. Do you know what's wrong? Thanks!
Marci Sarwan ([email protected])
Marci,
Did you add the Common Dialog Component to the project and then add the Common Dialog object to the form ?
I re-created your error in a new project, but after adding the component and object everything works fine.
Dim has provided error handling to rid any errors, including that one Marci. You should not receive any type of errors. Make sure you have the line On Error Resume Next or, as Dim has, On Error GoTo User_Cancelled.Quote:
Originally posted by Marci S
Hello Dim,
I tried your code and it crashes at:
".CancelError = True"
and says "Object Required"
I don't know what's wrong but I tested the code on a new project and it works but when I incorporate it into my program it crashes on the line above. Do you know what's wrong? Thanks!
Marci Sarwan ([email protected])
The WITH statement should have taken care of the "object required" thingy - are you
sure that you have that in your code or that your Common Dialog control is not named
something other than CommonDialog1?