The Common Dialog Box
description
Transcript of The Common Dialog Box
The Common Dialog Box
Installing the Common Dialog Box
May NOT be your standard VB toolbox.
Step 1:Select Components from the Project Menu
Step 2: Check Microsoft CommonDialog Control 6.0
The Many Methods of the Dialog Box
Open File .ShowOpen
Save File .ShowSave
Color .ShowColor
Print .ShowPrinter
Help .ShowHelp
Common Dialog (Example)
Interface
Set the Properties
txtDisplay cmdOpenFile cmdSaveFile cmdFont cmdPrint cmdTextColor cmdBackColor
Write the Code: Error Checking
CommonDialog1.CancelError = False On Error GoTo FontError ‘where to go…The code goes here… and here
FontError: ‘this is a labelExit SubFontError:End Sub
The Font Dialog: Flags
The of the Common Dialog box uses other flags to control behaviors. The Font dialog Flags property must be set to the value 1 to load the system fonts.After the font dialog box appears and selections are made, the values the user selected are available to the program.
Font Psuedocode
Set Cancel Error flag to 1Error handlerSet Flags property to 1Display the dialog boxSet font properties for bold, italic, font
strike through font size, font name, and properties of text box (example: txtFontBold = CommonDialog1.FontBold)
Shortcut Rather than typing txtFontBold = CommonDialog1.FontBoldtxtFontItalic = CommonDialog1.FontItalicEtc.Use the shortcutWith txtDisplay.Font
.Bold = CommonDialog1.FontBold
.Italic = CommonDialog1.FontItalicetc.
End With
Font CodeCommonDialog1.CancelError = FalseOn Error GoTo FontErrorWith CommonDialog1
.FontName = txtWord.FontName
.Flags = 1
.ShowFonttxtWord.Font.Name = .FontNametxtWord.FontBold = .FontBoldtxtWord.FontItalic = .FontItalictxtWord.FontSize = .FontSize
End WithExit SubFontError:
The Color Dialog
set CancelError to trueError handlerdisplay the color dialog boxset the text box property (eitherBackColor or ForeColor) to CommonDialog1.ColorError routine: no code needed.
How Files Work
Buffer Disk Drive
Computer
The Open File Dialog
Filter begins and ends with a quote marks. The pipe | is used as a separator.
"Text Files (*.txt) | *.txt| All Files (*.*) |*.*|HTML Files (*.htm)|*.htm*"
The Open Dialog:Getting the File Name
With CommonDialog1set the filterset the Filter index = 1display Open common dialogset FileName to .FileName
End With
The Open Dialog:Reading the File
Declare integer for bufferAssign bufferOpen the file (FileName) For
InputRead in the whole file
Input(LOF(F), F to the text boxClose the file
Open Dialog: CodeOn Error GoTo OpenErrorDim F As IntegerF = FreeFileCommonDialog1.CancelError = TrueCommonDialog1.Filter = "Text Files (*.txt)|
*.txt| Web Files (*.htm)|*.htm" CommonDialog1.ShowOpenOpen CommonDialog1.FileName For Input As FtxtWord.Text = Input(LOF(F), F)Close #F
Saving A FileSet Cancel ErrorError HandlerDim F for buffer numberset the filterset the filter index to 1show the save boxset file name to dialog box file nameOpen the file for outputPrint #F, txtWord.TextClose the fileF
The Print Dialog
CommonDialog1.ShowPrinter Some of the properties returned are:
FromPage ToPageToPage Copies
To Print
Use the object's Print method
Printer.Print <what you want printed>
Printer.EndDoc To print the contents oftext boxPrinter.Print txtWord.TextPrinter.EndDoc
Save or Save As?
If (the file has a name)save using the filename
elseshow the Save dialog
End If
You must:
Form_Load - Set FileName to "“mnuSaveAs - Set FileName to CommonDialog1.FileName mnuOpen - Set FileName to CommonDialog1.FileName mnuNew - Set FileName to ""
And in conclusion…
You should now be able to add save, save as, open, print, and set font to your word processor!