' Using the Common Dialog control - ShowOpen, ShowSave.
'
' Jon Vote
'
' 02/2002
'
' A simple text editor that demonstrates the use of the Common Dialog control.
'
' 1) Create a new project. Form1 will be created by default.
' 2) Select Project|Components from the menu and add the Microsoft Common Dialog Control to the project.
' 3) Add a Common Dialog Control to the form.
' 4) Add a text box to the form. Size it so it takes up most of the form.
' 5) Using the Properties Window (F4) set the MultiLine property of the text box to True.
' 6) Add 4 command buttons below the text box.
' 7) Paste the following code into the declarations section of Form1:
' --- Begin code for Form1
' Using the Common Dialog control - ShowOpen, ShowSave.
'
' A simple text editor that demonstrates the use of the Common Dialog control.
'
' 1) Create a new project. Form1 will be created by default.
' 2) Select Project|Components from the menu and add the
' Microsoft Common Dialog Control to the project.
' 3) Add a Common Dialog Control to the form.
' 4) Add a text box to the form. Size it so it takes up most of the form.
' 5) Using the Properties Window (F4) set the MultiLine property of the text box to True.
' 6) Add 4 command buttons below the text box.
' 7) Paste the following code into the declarations section of Form1:
' --- Begin code for Form1
Option Explicit
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" _
(ByVal lpFileName As String) As Long
Private Const CDLG_CANCEL_SELCTED = 32755
Private Sub Form_Load()
Command1.Caption = "&Open"
Command2.Caption = "&Save"
Command2.Enabled = False
Command3.Caption = "Save &As"
Command4.Caption = "E&xit"
CommonDialog1.CancelError = True
CommonDialog1.Filter = "Text Files(*.txt)|*.txt|" & _
"HTML Files(*.htm*)|*.htm*|" & _
"ASP Files(*.asp)|*.asp|All Files(*.*)|*.*"
CommonDialog1.Flags = cdlOFNHideReadOnly
Text1.Text = ""
Me.Caption = "CommonDialog Example"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Command1_Click()
Dim f As Integer
On Error GoTo err_Command1_Click
CommonDialog1.ShowOpen
Screen.MousePointer = vbHourglass
f = FreeFile
Open CommonDialog1.FileName For Input As #f
Text1.Text = Input$(LOF(f), f)
Close f
EnableCommands
Screen.MousePointer = vbDefault
Exit Sub
err_Command1_Click:
Screen.MousePointer = vbDefault
If Err.Number <> CDLG_CANCEL_SELCTED Then
MsgBox Err.Description
End If
End Sub
Private Sub Command2_Click()
Dim strMessage As String
Dim f As Integer
On Error GoTo err_Command2_Click
Screen.MousePointer = vbHourglass
strMessage = "Warning - this will replace "
strMessage = strMessage & CommonDialog1.FileName
strMessage = strMessage & " with the current contents of the text box."
strMessage = strMessage & vbCrLf & "Are you sure you want to continue?"
If MsgBox(strMessage, vbOKCancel, "Warning this is not a drill!") = vbOK Then
f = FreeFile
Open CommonDialog1.FileName For Output As #f
Print #f, Text1.Text
Close f
End If
Screen.MousePointer = vbDefault
Exit Sub
err_Command2_Click:
MsgBox Err.Description
End Sub
Private Sub Command3_Click()
Dim f As Integer
On Error GoTo Command3_Click
CommonDialog1.ShowSave
If GetFileAttributes(CommonDialog1.FileName) > 0 Then
If MsgBox("File exists - ok to overwrite?", _
vbOKCancel, "Yike! File Exists!!") <> vbOK Then
Exit Sub
End If
End If
'New file or ok to overwrite here
f = FreeFile
Open CommonDialog1.FileName For Output As #f
Print #f, Text1.Text
Close f
Exit Sub
Command3_Click:
If Err.Number <> CDLG_CANCEL_SELCTED Then
MsgBox Err.Description
End If
End Sub
Private Sub Command4_Click()
Unload Me
End Sub
Private Sub EnableCommands()
Dim ctlControl As Control
For Each ctlControl In Me.Controls
If TypeOf ctlControl Is CommandButton Then
ctlControl.Enabled = True
End If
Next ctlControl
End Sub
' --- End code for Form1 ---