I am writting some code using the microsoft common dialog control 6.0 and need to have an action take place when someone clicks on open or cancel. How do you write code for an action when you click cancel or open?
Thanks
Printable View
I am writting some code using the microsoft common dialog control 6.0 and need to have an action take place when someone clicks on open or cancel. How do you write code for an action when you click cancel or open?
Thanks
FROM MSDN
Visual Basic: CommonDialog Control
CommonDialog Control
The CommonDialog control provides a standard set of dialog boxes for operations such as opening and saving files, setting print options, and selecting colors and fonts. The control also has the ability to display help by running the Windows Help engine.
Syntax
CommonDialog
Remarks
The CommonDialog control provides an interface between Visual Basic and the routines in the Microsoft Windows dynamic-link library Commdlg.dll. To create a dialog box using this control, Commdlg.dll must be in your Microsoft Windows SYSTEM directory.
You use the CommonDialog control in your application by adding it to a form and setting its properties. The dialog displayed by the control is determined by the methods of the control. Atrun time, a dialog box is displayed or the help engine is executed, when the appropriate method is invoked; atdesign time, the CommonDialog control is displayed as an icon on a form. This icon can't be sized.
The CommonDialog control can display the following dialogs using the specified method.
Method Dialog Displayed
ShowOpen Show Open Dialog Box
ShowSave Show Save As Dialog Box
ShowColor Show Color Dialog Box
ShowFont Show Font Dialog Box
ShowPrinter Show Print or Print Options Dialog Box
ShowHelp Invokes the Windows Help Engine
The CommonDialog control automatically provides context sensitive help on the interface of the dialog boxes by clicking:
The What's This help button in the title bar then clicking the item for which you want more information.
The right mouse button over the item for which you want more information then selecting the What's This command in the displayed context menu.
The operating system provides the text shown in the Windows 95 (or later) Help popup. You can also display a Help button on the dialog boxes with the CommonDialog control by setting the Flags property, however, you must provide the help topics in this situation.
Note There is no way to specify where a dialog box is displayed.
For More Information To see help topics for each dialog, click on See Also.
VB Code:
Set the CommandDialog1's CancelError property to True Private Sub Command1_Click() On Error GoTo Hell: CommandDialog1.ShowOpen 'Shows open dialog Msgbox CommandDialog1.FileName 'Shows the selected file.. This code will only executed when Open is clicked Exit Sub Hell: 'This is where the control comes when user clicks Cancel Button If Err.Number = 32755 Then 'U check to see if the error is one generated by the Cancel Button ''Code.. End If End Sub
Something like this:
RoyVB Code:
Private Sub Form_Load() '======================= Dim strFileName As String On Error GoTo ErrHanler CD.ShowOpen If Not CD.CancelError Then strFileName = CD.FileName End If Exit Sub ErrHanler: '--------------- Debug.Print Err.Description Err.Clear Resume Next End Sub
Thanks Swatty, great information!
Is there a way to have a second action take place when you click on a common diaglogue button, for example if you click on open can you also trigger the form to change color.
Yah Sure !!!
VB Code:
Set the CommandDialog1's CancelError property to True Private Sub Command1_Click() On Error GoTo Hell: CommandDialog1.ShowOpen 'Shows open dialog Me.BackColor = vbBlue '.. This code will only executed when Open is clicked Exit Sub Hell: 'This is where the control comes when user clicks Cancel Button If Err.Number = 32755 Then 'U check to see if the error is one generated by the Cancel Button ''Code.. End If End Sub
Where can I find info on Err.Number = 32755?
In general, it is not a good practce to hardcode an error number (or anything at all if can be avoided). Instead use (as much as you can) generic Error Handling routine.