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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I improve control over the MS common dialog?

Tips -N- Tricks

How can I improve control over the MS common dialog?

by  ChrisRChamberlain  Posted    (Edited  )
One of the useful features of the MS common dialog is the facility to select multiple files, and an alternative example is outlined in faq184-3106.

What is not so easy is to control where on the screen you would like the dialog to appear and to add an icon specific to your application to the dialog.

To begin, add a new form to your project, setting [color blue].Visible = .F.[/color], [color blue].ShowWindow = 2[/color], and set the [color blue].Icon[/color] property to a suitable icon.

What the form will do is 'launch' the Microsoft Common Dialog Control where you, as a developer, wish it be seen, whilst the form itself never shows.

Go to Tools > Options > Controls > ActiveX Controls, select Microsoft Common Dialog Control, Version 6.0, and drop it on your form.

In the [color blue].Activate()[/color] event of the form add
Code:
[color blue]THIS.Release()[/color]

In the [color blue].Init()[/color] event of the form, add the following code
Code:
[color blue]LOCAL lcCurDir ,;
[tab]lcFileName ,;
[tab]lcFileList ,;
[tab]lcPath

lcCurDir = CURDIR()

WITH THIS
[tab].Left = 350
[tab].Top = 425
[tab].olecontrol1.Filter = [All Files|*.*|JPG Files|*.jpg|BMP Files|*.bmp]
[tab].olecontrol1.FilterIndex = 1
[tab].olecontrol1.DialogTitle = [Open files]
[tab].olecontrol1.MaxFileSize = 32767
[tab].olecontrol1.Flags = BITOR(0x4, 0x10, 0x200, 0x800, 0x1000, 0x8000, 0x40000, 0x80000)
[tab].olecontrol1.InitDir = [C:\]
[tab].olecontrol1.ShowOpen()
[tab]lcFilelist = .olecontrol1.FileName
[tab]lcFileName = []
[tab]lcPath = []
ENDWITH 	

CREATE CURSOR FILENAMES (filename C(254))

DO CASE
CASE EMPTY(lcFileList)
[tab]USE IN SELECT([FILENAMES])
CASE OCCURS(CHR(0),lcFileList) = 0
[tab]INSERT INTO FILENAMES (filename) VALUES (lcFileList)
OTHERWISE
[tab]FOR i = 0 TO OCCURS(CHR(0),lcFileList)
[tab][tab]IF i = 0 
[tab][tab][tab]lcPath = SUBSTR(lcFileList,1,AT(CHR(0),lcFileList))		
[tab][tab][tab]lcFileList = SUBSTR(lcFileList,AT(CHR(0),lcFileList) + 1)
[tab][tab]ELSE
[tab][tab][tab]IF CHR(0) $ lcFileList	
[tab][tab][tab][tab]lcFileName = SUBSTR(lcFileList,1,AT(CHR(0),lcFileList))	
[tab][tab][tab][tab]lcFileList = SUBSTR(lcFileList,AT(CHR(0),lcFileList) + 1)	
[tab][tab][tab]ELSE
[tab][tab][tab][tab]lcFileName = lcFileList
[tab][tab][tab]ENDI
[tab][tab][tab]INSERT INTO FILENAMES	;
[tab][tab][tab][tab](filename);
[tab][tab][tab][tab]VALUES ;
[tab][tab][tab][tab](ADDBS(lcPath) + lcFileName)
[tab][tab]ENDIF
[tab]ENDFOR
ENDCASE

SET DEFA TO (lcCurDir)[/color]
The use of the [color blue]BITOR()[/color] function is an alternative to adding the desired constant values together to determine the [color blue].Flags[/color] value, which in turn determines the behaviour of the common dialog.

Documentation may be found [link http://msdn.microsoft.com/en-us/library/Aa238842]here[/link], and there may well be better documentation elsewhere.

It's relatively simple to change the behaviour of the common dialog by adding or removing parameters to suit.

Other properties will also need modifying to suit your particular project.

The dialog will now appear wherever you have determined the form would have shown and the dialog will have the icon of your choice.

Unless the user cancelled out, the barebones code example will populate a cursor FILENAMES with the path\filename.ext of a single selected file or path\filename.ext of multiple selected files.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top