Visual Basic 6 Black Book:Windows Common Dialogs
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous Table of Contents Next
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 controls ShowOpen and ShowSave methods. These methods need no arguments passed to themto set various options, you set the Common Dialog controls 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 Dialogs 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 files name, use the FileTitle property. Lets see an example. In this case, well let the user select a file to open, and then display the files name and path in a message box. Start by adding a Common Dialog control to a form, then set the controls 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, as in Figure 11.6.
Figure 11.6 The Open dialog box. 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 controls 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 doesnt 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 isnt 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 wont have the Read Only attribute set and wont 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 were doing numerically, its usually better to use constants to make your code more readable.) Adding the desired constant values produces the same result.