SaveFileDialog


The SaveFileDialog component displays a dialog box that lets the user select a file for saving. The ShowDialog method returns DialogResult.OK if the user selects a file and clicks OK. It returns DialogResult.Cancel if the user cancels. The following code shows how a program might use the component to let the user select a file in which to save data:

  If dlgSaveData.ShowDialog = DialogResult.OK Then     ' Save the data.     ... End If 

This component provides many properties for determining the kinds of files the user can specify. Most of these properties are the same as those provided by the OpenFileDialog component described earlier in this appendix. See the section “OpenFileDialog” earlier in this appendix for more information about those properties.

Unlike OpenFileDialog, this component does not provide the properties MultiSelect, ReadOnlyChecked, and ShowReadOnly because those properties don’t make sense when the user is selecting a file for saving. The FileNames collection is also less useful for this component because the user will always select only one file, so you can use the FileName property instead.

The SaveFileDialog component provides one additional property not provided by the OpenFileDialog: CreatePrompt. If this property is True and the user enters the name of a file that doesn’t exist, then the dialog asks the user if it should create the file. If the user clicks No, then the dialog box continues letting the user select a different file.

Like the OpenFileDialog, this component raises its FileOk event when the user tries to accept a file. You can use an event handler to catch the event and perform extra validation. Set the event’s e.Cancel value to True to stop the dialog box from accepting the selection.

The following code only allows the dlgSaveData dialog box to accept files that end with the .dat extension. If the dialog box’s selected file doesn’t end with .dat, the event handler sets e.Cancel to True.

  ' Ensure that the user selects a .dat file. Private Sub dlgSaveData_FileOk(ByVal sender As Object, _  ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgSaveData.FileOk     If Not dlgSaveData.FileName.EndsWith(".dat") Then         MsgBox("File " & dlgSaveData.FileName & _             " is not a .dat file", _             MsgBoxStyle.Exclamation, _             "Invalid File Type")         e.Cancel = True     End If End Sub 

Note that the dialog box adds its default extension if applicable before it raises the FileOk event. If the component has DefaultExt = “dat” and AddExtension = True, then this example would accept a file name with no extension.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net