Search In Site

27 June, 2013

Open And Save As Dialog boxes in Vb

Using The Open And Save As Dialogs
Probably the most common use of the Common Dialog control is to display File Open and File Save As dialog boxes, and you display those dialog boxes with the Common Dialog control's ShowOpen and ShowSave methods. These methods need no arguments passed to them to set various options, you set the Common Dialog control's Flags property (see the next topic), such as overwriting existing files and so on.
You can also set the Filter property so the dialog box displays only certain types of files, such as text files. See Setting File Types (Filters) In Open, Save As Dialogs, a little later in this chapter. To find out what file the user wants to work with, you check the Common Dialog's FileName property after the user clicks on OK in the dialog box. That property holds the fully qualified (that is, with path) name of the file to open. If you just want the file's name, use the FileTitle property. Let's see an example. In this case, we'll let the user select a file to open, and then display the file's name and path in a message box. Start by adding a Common Dialog control to a form, then set the control's CancelError property to True so we can check if the user clicked Cancel. To check that, we use On Error GoTo: 

Private Sub Command1_Click()
On Error GoTo Cancel
...
Cancel:
End Sub

Then we display the Open dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
...
Cancel:
End Sub

Finally, assuming the user clicked on OK, we can display the name of the file they selected in a message box using the FileName property:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
MsgBox "File to open: " & CommonDialog1.FileName
Cancel:
End Sub

When you run this code and click the button, the Open dialog box appears.
If you make a file selection and click on OK, the Open dialog box closes and the program displays the name of the file you selected, along with its path, in a message box. Our program is a success; the code for this program is located in the opendialog folder on this books accompanying CD-ROM.
Setting Open And Save As Flags
You can set a wide variety of options when you display File Open and File Save As dialog boxes by setting the Common Dialog control's Flags property. Here are the possible settings:
" cdlOFNAllowMultiselect_&H200; specifies that the File Name list box allows multiple selections.
" cdlOFNCreatePrompt_&H2000; the user can select more than one file at runtime by pressing the Shift key and using the up arrow and down arrow keys to select the desired files. When this is done, the FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces.
" cdlOFNCreatePrompt_&H2000; specifies that the dialog box prompts the user to create a file that doesn't currently exist. This flag automatically sets the
cdlOFNPathMustExist and cdlOFNFileMustExist flags.
" cdlOFNExplorer_&H80000; displays the Explorer-like Open A File dialog box template. Works with Windows 95 and Windows NT 4.
" cdlOFNExtensionDifferent_&H400; indicates that the extension of the returned file name is different from the extension specified by the DefaultExt property. This flag isn_t set if the DefaultExt property is Null, if the extensions match, or if the file has no extension. This flag value can be checked upon closing the dialog box. This can be useful if you want to track the kind of file the user wants to open.
" cdlOFNFileMustExist_&H1000; specifies that the user can enter only names of existing files in the File Name text box. If this flag is set and the user enters an invalid file name, a warning is displayed. This flag automatically sets the cdlOFNPathMustExist flag.
" cdlOFNHelpButton_&H10; causes the dialog box to display the Help button.
" cdlOFNHideReadOnly_&H4; hides the Read Only checkbox.
" cdlOFNLongNames_&H200000; enables the use of long file names.
" cdlOFNNoChangeDir_&H8; forces the dialog box to set the current directory to what it was when the dialog box was opened.
" cdlOFNNoDereferenceLinks_&H100000; disables the use of shell links (also known as shortcuts). By default, choosing a shell link causes it to be interpreted by the shell.
" cdlOFNNoLongNames_&H40000; disables long file names.
" cdlOFNNoReadOnlyReturn_&H8000; specifies that the returned file won't have the Read Only attribute set and won't be in a write-protected directory.
" cdlOFNNoValidate_&H100; specifies that the Common Dialog allows invalid characters in the returned file name.
" cdlOFNOverwritePrompt_&H2; 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.
" cdlOFNPathMustExist_&H800; specifies that the user can enter only valid paths. If this flag is set and the user enters an invalid path, a warning message is displayed.
" cdlOFNReadOnly_&H1; causes the Read Only checkbox to be initially checked when the dialog box is created. This flag also indicates the state of the Read Only checkbox when the dialog box is closed.
" cdlOFNShareAware_&H4000; specifies that sharing violation errors will be ignored.
You can set more than one flag for a dialog box using the Or operator. For example:
CommonDialog1.Flags = &H10& Or &H200&
(Although this shows what we're doing numerically, it's usually better to use constants to make your code more readable.) Adding the desired constant values produces the same result.

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget