Results 1 to 13 of 13

Thread: common dialog (its really easy, come get a postcount!)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    How do i load a file with the common dialog controls (never learned how)

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    thanks, only 7 mnutes to respond!!!!!!!! thanks da_silvey

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    uh, how do i use this?

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    add a common dialog box then add this code to command button 1

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    or do you need help with something else?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    yeah how do I make it load a pic, there is nothing that specifies the filename or what was chosen

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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?

  9. #9
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    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
    Or if you need to load a picture into a picturebox (from what i could understand) then you would use:
    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
    Hope that helps,
    D!m
    Dim

  10. #10
    Member
    Join Date
    Jun 2000
    Location
    Posts
    52
    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 Sarwan

  11. #11
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    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.


  12. #12
    Guest
    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])
    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.

  13. #13
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    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?
    Donald Sy - VB (ab)user

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width