Table 2.1 Simple Visual Basic Data Types

advertisement
Adding Common Dialog Control


Not on
standard
Toolbox
To add this
custom
control,
select
Components
from Project
menu
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Common Dialog Control

Custom control that allows VB program
to use dialog boxes provided with
Windows
Color



File
Font
Print
Hungarian notation:
dlgNameOfControl
Size of dialog box is controlled by
Windows and cannot be changed by
program
Only one control is needed to access
all standard dialog boxes by the form
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Control Color: Common Dialog


Place common dialog control on form (it remains
invisible during execution)
To allow the user to control the color aspect of one or
more objects, write an event procedure (e.g.,
mnuEditColor_Click) with the following logic
1) Assign defaults to dialog box:
dlgName.Flags = cdlCCRGBInit
‘ initialize dialog box
dlgName.Color = frmName.BackColor ‘ assign backcolor on form
2) Display the desired standard dialog box to user:
Call dlgName.ShowColor
3) Apply information from dialog box selected by user to
program:
frmName.BackColor = dlgName.Color
lblNumber.ForeColor = dlgName.Color
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Control Font: Common Dialog


Place common dialog control on form (it remains invisible during
execution)
To allow the user to control the font aspect of one or more objects, write
an event procedure (e.g., mnuEditFont_Click) with the following logic
1) Assign defaults to dialog box:
dlgName.FontBold = lblNumber.Font.Bold
dlgName.Flags = cdlCFScreenFonts ‘ install fonts to show in list box
‘ see below to include color, strikethru & underline on font dialog
dlgName.Flags = cdlCFEffects Or cdlCFScreenFonts
2) Display the desired standard dialog box to user:
Call dlgName.ShowFont
3) Apply information from dialog box as selected by user to program:
lblNumber.Font.Name = dlgName.FontName
lblNumber.Font.Bold = dlgName.FontBold
lblNumber.Font.Italic = dlgName.FontItalic
lblNumber.ForeColor = dlgName.Color
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Control File Access: Common Dialog


Place common dialog control on form (it remains invisible during
execution)
Example: mnuOpenPicture_Click lets user pick image
1)
Optional: adjust window parameters to better inform user:
dlgCommon.Flags = cdlOFNFileMustExist ' file must exist flag
dlgCommon.DefaultExt = ".gif"
' default extension .gif
dlgCommon.Filter = ”Picture (*.gif)| *.gif" ' filter show these files
' customize the title bar of the dialog box
dlgCommon.DialogTitle = "OPEN--Please select picture file”
2) Display the desired standard dialog box to user:
Call dlgName.ShowOpen
3)
To use information from dialog box selected by user:
imgFlag.Picture = LoadPicture(dlgName.FileName)
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Updated Colorful Hello World
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Updated Colorful Hello World

Allowing the user to change background colors
' Purpose: The Color Palette appears with the current options shown as
'
defaults. Any changes made by the user appear on the form.
Private Sub mnuEditChangeBackColor_Click()
dlgCommon.Flags = cdlCCRGBInit
' initialize dialog box
dlgCommon.Color = frmMain.BackColor
' assign backcolor on form
Call dlgCommon.ShowColor
frmMain.BackColor = dlgCommon.Color
fraLanguage.BackColor = dlgCommon.Color
optEnglish.BackColor = dlgCommon.Color
optSpanish.BackColor = dlgCommon.Color
optHindi.BackColor = dlgCommon.Color
End Sub
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Updated Colorful Hello World

Allowing the user to change font styles
' Purpose: The Font dialog appears with the current options shown as defaults.
'
Any changes made by the user are reflected in the message label.
Private Sub mnuEditChangeFont_Click()
dlgCommon.FontName = lblMessage.Font.Name
dlgCommon.FontBold = lblMessage.Font.Bold
dlgCommon.FontItalic = lblMessage.Font.Italic
dlgCommon.FontSize = lblMessage.Font.Size
dlgCommon.Color = lblMessage.ForeColor
dlgCommon.FontStrikethru = lblMessage.Font.Strikethrough
dlgCommon.FontUnderline = lblMessage.Font.Underline
dlgCommon.Flags = cdlCFScreenFonts
' install fonts to show in list box
' see below to include color, strikethru & underline on font dialog
dlgCommon.Flags = cdlCFEffects Or cdlCFScreenFonts
Call dlgCommon.ShowFont
lblMessage.Font.Name = dlgCommon.FontName
lblMessage.Font.Bold = dlgCommon.FontBold
lblMessage.Font.Italic = dlgCommon.FontItalic
lblMessage.Font.Size = dlgCommon.FontSize
lblMessage.ForeColor = dlgCommon.Color
lblMessage.Font.Strikethrough = dlgCommon.FontStrikethru
lblMessage.Font.Underline = dlgCommon.FontUnderline
End Sub
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Updated Colorful Hello World

Allow the user to view a different picture
' Purpose: The File open dialog appears to allow the user to select a new
'
image for the form.
Private Sub mnuFileChangePicture_Click()
dlgCommon.Flags = cdlOFNFileMustExist
' file must exist flag
dlgCommon.DefaultExt = ".gif"
' default extension .gif
dlgCommon.Filter = "Picture (*.gif)| *.gif"
' filter show the files
' customize the title bar of the dialog box
dlgCommon.DialogTitle = "OPEN--Please select new picture file”
Call dlgCommon.ShowOpen
imgFlag.Picture = LoadPicture(dlgCommon.FileName)
End Sub
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Use Error Handler in Hello World

Make the Flags property assignment line a comment
' Purpose: The File open dialog appears to allow the user to select a new
'
image for the form.
Private Sub mnuFileChangePicture_Click()
On Error GoTo ErrHandler
'
dlgCommon.Flags = cdlOFNFileMustExist
' file must exist flag
dlgCommon.DefaultExt = ".gif"
' default extension .gif
dlgCommon.Filter = "Picture (*.gif)| *.gif"
' filter show the files
' customize the title bar of the dialog box
dlgCommon.DialogTitle = "OPEN--Please select new picture file”
Call dlgCommon.ShowOpen
imgFlag.Picture = LoadPicture(dlgCommon.FileName)
Exit Sub
ErrHandler:
Call MsgBox("Error #" & Err.Number & ": " & Err.Description, _
vbOKOnly + vbExclamation)
End Sub
© 1999, by Que Education and Training, Appendix A, pages 769-776 of
Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach
Download