Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Common Dialog Control

Status
Not open for further replies.

allyeric

Programmer
Mar 14, 2000
104
CA
I would like to use the common dialog control to get a picture file from the user, and insert the picture into an image box on the form.&nbsp;&nbsp;I am not quite sure how to use this control, and I have searched MSDN, but, unfortunately, there is not much information there.&nbsp;&nbsp;Can someone please help?&nbsp;&nbsp;And, maybe recommend a site where I can learn more about this?<br><br>Thanks in advance
 
In a nutshell:<br><br>after putting a commondialog on your form,<br>call commondialog1.showopen.&nbsp;&nbsp;After the user has selected a file, it will be in commondialog1.filename.&nbsp;&nbsp;You'll probably want to adjust the flags property before you open the commondialog.<br><br><br><br> <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
I kinda figured that part out so far - but when I try that I get errors.&nbsp;&nbsp;Here is my code:<br><br><br>cdComDia.ShowOpen<br><br>OpenFile (cdComDia.FileName)<br>&nbsp;&nbsp;&nbsp;<br>imgPicture.Picture = cdComDia.FileName<br><br><br>when I use this, it give me the error: wrong number of arguments or invalid property assignment&nbsp;&nbsp;(this error is on the OpenFile line)<br><br>If I remove that line, I get this error:&nbsp;&nbsp;type mismatch&nbsp;&nbsp;(this error is on .FileName)<br><br>It looked pretty easy when I started, but not as easy as it looked.&nbsp;&nbsp;Perhaps there is another way to achieve what I want to do here?<br><br><br>
 
Try this instead:<br><br>'Set Flags Here<br>'cdlDialog.Flags = ...<br>cdlDialog.ShowOpen<br>imgPicture.Picture = LoadPicture(cdlDialog.FileName)<br><br>(You'll still need some error handling in case the file is not a valid image file, or in case it can't be read.)<br><br>Kenny Acock<br>Teacher's Pal, Inc.<br><A HREF="mailto:kennya@teacherspal.com">kennya@teacherspal.com</A>
 
Another thing you'll need to do is to deal with the fact that the user might cancel out of the common dialog.&nbsp;&nbsp;You'll want to check out the cancelerror property, and add a handler for this case.<br> <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
thank you both for your reply - I am trying to figure out the flags now.&nbsp;&nbsp;Is there another - easier - way to accomplish what I need to do here?&nbsp;&nbsp;And, if not, can you perhaps suggest a site where I can learn more about this control, and the flags?<br><br><br>
 
Use this function if you won't give an ocx with your program.Many people don't like have many *.ocx on their PC<br><br>Public Type OPENFILENAME<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lStructSize As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hwndOwner As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hInstance As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrFilter As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrCustomFilter As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nMaxCustFilter As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFilterIndex As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrFile As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nMaxFile As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrFileTitle As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nMaxFileTitle As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrInitialDir As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrTitle As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flags As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFileOffset As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFileExtension As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpstrDefExt As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lCustData As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpfnHook As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpTemplateName As String<br>End Type<br>'<br>Public Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetOpenFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long<br>Public Declare Function GetSaveFileName Lib &quot;comdlg32.dll&quot; Alias &quot;GetSaveFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long<br><br>'<br>' Constant flags<br>Public Const OFN_ALLOWMULTISELECT = &H200<br>Public Const OFN_CREATEPROMPT = &H2000<br>Public Const OFN_ENABLEHOOK = &H20<br>Public Const OFN_ENABLETEMPLATE = &H40<br>Public Const OFN_ENABLETEMPLATEHANDLE = &H80<br>Public Const OFN_EXPLORER = &H80000<br>Public Const OFN_EXTENSIONDIFFERENT = &H400<br>Public Const OFN_FILEMUSTEXIST = &H1000<br>Public Const OFN_HIDEREADONLY = &H4<br>Public Const OFN_LONGNAMES = &H200000<br>Public Const OFN_NOCHANGEDIR = &H8<br>Public Const OFN_NODEREFERENCELINKS = &H100000<br>Public Const OFN_NOLONGNAMES = &H40000<br>Public Const OFN_NONETWORKBUTTON = &H20000<br>Public Const OFN_NOREADONLYRETURN = &H8000<br>Public Const OFN_NOTESTFILECREATE = &H10000<br>Public Const OFN_NOVALIDATE = &H100<br>Public Const OFN_OVERWRITEPROMPT = &H2<br>Public Const OFN_PATHMUSTEXIST = &H800<br>Public Const OFN_READONLY = &H1<br>Public Const OFN_SHAREAWARE = &H4000<br>Public Const OFN_SHAREFALLTHROUGH = 2<br>Public Const OFN_SHARENOWARN = 1<br>Public Const OFN_SHAREWARN = 0<br>Public Const OFN_SHOWHELP = &H10<br>'<br>Global Dialogue As OPENFILENAME<br><br>Public Function Dlg(Title As String, Filter As String, Hwnd As Long) As String<br><br><br>Dim Fichier As String<br>Dim Filtre As String<br>Dim RetVal As Long<br>Dialogue.lStructSize = Len(Dialogue)<br>Dialogue.hwndOwner = Hwnd<br>Dialogue.hInstance = App.hInstance<br>Dialogue.lpstrFilter = Filter<br>Dialogue.lpstrFile = Space(254)<br>Dialogue.nMaxFile = 255<br>Dialogue.lpstrFileTitle = Space(254)<br>Dialogue.nMaxFileTitle = 255<br>Dialogue.lpstrInitialDir = App.Path<br>Dialogue.lpstrTitle = Title<br>Dialogue.flags = 6148<br>'<br>RetVal = GetOpenFileName(Dialogue)<br>If RetVal = 0 Then<br>Dlg = &quot;&quot;<br>Else<br>Dlg = Dialogue.lpstrFile<br>End If<br>End Function<br><br>If you want some help about the flags look at it :<br><br>Flag Meaning<br>OFN_ALLOWMULTISELECT <br>Specifies that the File Name list box allows multiple selections. If you also set the OFN_EXPLORER flag, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface. If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the filenames of the selected files. The nFileOffset member is the offset to the first filename, and the nFileExtension member is not used. For Explorer-style dialog boxes, the directory and filename strings are NULL separated, with an extra NULL character after the last filename. This format enables the Explorer-style dialogs to return long filenames that include spaces. For old-style dialog boxes, the directory and filename strings are separated by spaces and the function uses short filenames for filenames with spaces. You can use the FindFirstFile function to convert between long and short filenames. If you specify a custom template for an old-style dialog box, the definition of the File Name list box must contain the LBS_EXTENDEDSEL value. <br>&nbsp;<br>&nbsp;OFN_CREATEPROMPT <br>If the user specifies a file that does not exist, this flag causes the dialog box to prompt the user for permission to create the file. If the user chooses to create the file, the dialog box closes and the function returns the specified name; otherwise, the dialog box remains open. <br>OFN_ENABLEHOOK <br>Enables the hook function specified in the lpfnHook member. <br>OFN_ENABLETEMPLATE <br>Indicates that the lpTemplateName member points to the name of a dialog template resource in the module identified by the hInstance member.If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.<br>OFN_ENABLETEMPLATEHANDLE <br>Indicates that the hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores the lpTemplateName if this flag is specified.If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.<br>OFN_EXPLORER <br>Indicates that any customizations made to the Open or Save As dialog box use the new Explorer-style customization methods. For more information, see the &quot;Explorer-Style Hook Procedures&quot; and &quot;Explorer-Style Custom Templates&quot; sections of the Common Dialog Box Library overview.By default, the Open and Save As dialog boxes use the Explorer-style user interface regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template, or set the OFN_ALLOWMULTISELECT flag. If you want the old-style user interface, omit the OFN_EXPLORER flag and provide a replacement old-style template or hook procedure. If you want the old style but do not need a custom template or hook procedure, simply provide a hook procedure that always returns FALSE.<br>OFN_EXTENSIONDIFFERENT <br>Specifies that the user typed a filename extension that differs from the extension specified by lpstrDefExt. The function does not use this flag if lpstrDefExt is NULL.<br>OFN_FILEMUSTEXIST <br>Specifies that the user can type only names of existing files in the File Name entry field. If this flag is specified and the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is also used.<br>OFN_HIDEREADONLY <br>Hides the Read Only check box.<br>OFN_LONGNAMES <br>For old-style dialog boxes, this flag causes the dialog box to use long filenames. If this flag is not specified, or if the OFN_ALLOWMULTISELECT flag is also set, old-style dialog boxes use short filenames (8.3 format) for filenames with spaces. Explorer-style dialog boxes ignore this flag and always display long filenames.<br>OFN_NOCHANGEDIR <br>Restores the current directory to its original value if the user changed the directory while searching for files.<br>OFN_NODEREFERENCELINKS <br>Directs the dialog box to return the path and filename of the selected shortcut (.LNK) file. If this value is not given, the dialog box returns the path and filename of the file referenced by the shortcut<br>OFN_NOLONGNAMES <br>For old-style dialog boxes, this flag causes the dialog box to use short filenames (8.3 format). Explorer-style dialog boxes ignore this flag and always display long filenames.<br>OFN_NONETWORKBUTTON <br>Hides and disables the Network button.<br>OFN_NOREADONLYRETURN <br>Specifies that the returned file does not have the Read Only check box checked and is not in a write-protected directory.<br>OFN_NOTESTFILECREATE <br>Specifies that the file is not created before the dialog box is closed. This flag should be specified if the application saves the file on a create-nonmodify network sharepoint. When an application specifies this flag, the library does not check for write protection, a full disk, an open drive door, or network protection. Applications using this flag must perform file operations carefully, because a file cannot be reopened once it is closed.<br>OFN_NOVALIDATE <br>Specifies that the common dialog boxes allow invalid characters in the returned filename. Typically, the calling application uses a hook procedure that checks the filename by using the FILEOKSTRING message. If the text box in the edit control is empty or contains nothing but spaces, the lists of files and directories are updated. If the text box in the edit control contains anything else, nFileOffset and nFileExtension are set to values generated by parsing the text. No default extension is added to the text, nor is text copied to the buffer specified by lpstrFileTitle.<br>If the value specified by nFileOffset is less than zero, the filename is invalid. Otherwise, the filename is valid, and nFileExtension and nFileOffset can be used as if the OFN_NOVALIDATE flag had not been specified.<br>OFN_OVERWRITEPROMPT <br>Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.<br>OFN_PATHMUSTEXIST <br>Specifies that the user can type only valid paths and filenames. If this flag is used and the user types an invalid path and filename in the File Name entry field, the dialog box function displays a warning in a message box.<br>OFN_READONLY <br>Causes the Read Only check box to be checked initially when the dialog box is created. This flag indicates the state of the Read Only check box when the dialog box is closed.<br>OFN_SHAREAWARE <br>Specifies that if a call to the OpenFile function fails because of a network sharing violation, the error is ignored and the dialog box returns the selected filename. If this flag is not set, the dialog box notifies your hook procedure when a network sharing violation occurs for the filename specified by the user. If you set the OFN_EXPLORER flag, the dialog box sends the CDN_SHAREVIOLATION message to the hook procedure. If you do not set OFN_EXPLORER, the dialog box sends the SHAREVISTRING registered message to the hook procedure. <br>OFN_SHOWHELP <br>Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button.An Explorer-style dialog box sends a CDN_HELP notification message to your hook procedure when the user clicks the Help button. <br><br><br>Send me remark at <A HREF="mailto:drone38@mageos.com">drone38@mageos.com</A>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top