Student Profile Sample - We help schools to connect the data they have, with ...
Common DialogControl and RichTextBox in Visual Basic
1. Common DialogControl
CommonDialogControl The commondialogbox invisual basicisan insertable control thatallows
usersto displayanumberof commondialogboxesintheirprogram.
These include OpenandSave Asfile dialogboxes;the FindandReplace editingdialogboxes;the
Print,PrintSetup,PrintPropertySheet,andPage Setupprintingdialogboxes;andthe Colorand Font
dialogboxes
.The easiestwaytoaccess these controlsistouse a componentcalledMicrosoftCommonDialog
Control 6.0. To addthiscomponenttoyour project, choose Project- Components,andmark it inthe
controlslist.Subsequently,anewcontrol shouldappearinyourcontrol toolbox.Choose it,andplace
the control onto yourform (note it'sonlyvisibleindesigntime).
Open and Save dialogs
ShowOpenshowsa dialogthat letsthe userchoose a drive,directory,file name andfile extension
to (presumably) openafile.
The save dialogisidentical inapperance andfunction,withthe exceptionof the caption.
At run time,whenthe userchoosesafile andclosesthe dialogbox,the FileName propertyisused
to getthe selectedfile name.
Before displayingthe dialogBefore youcanuse these dialogs,youmust firstsetthe Filterproperty
to allowthe userto selectafile extension .The filterpropertyisastringwiththe followingstructure:
description1|filter1|description2|filter2|
Here,the descriptionisthe stringthatshowsupin the listbox of all the available filterstochoose
from.
setdefaultextensionusing DefaultExt).
Setdefaultinitial directoryusing initdir
code
COMMONDIALOG1.Filter="textfiles|*.txt|documents|*.doc|allfiles|*.*"
COMMONDIALOG1.FileName="text1"
COMMONDIALOG1.InitDir= "D:"
COMMONDIALOG1.ShowOpen
RICHTEXTBOX1.FileName = COMMONDIALOG1.FileName
Color dialog
The color dialogallowsthe usertoselectacolor fromboth a palette aswell asthroughmanual
choosingusingRGB .
2. You retrieve the selectedcolorusingthe Colorproperty.
Flags usedin color dialog
Constant Value Description
cdlCCFullOpen &H2 Entire dialog box is displayed, including the Define Custom Colors
section.
cdlCCHelpButton &H8 Causes the dialog box to display a Help button.
cdlCCPreventFullOpen &H4 Disables the Define Custom Colors command button and prevents
the user from defining custom colors.
cdlCCRGBInit &H1 Sets the initial color value for the dialog box.
Sample code
COMMONDIALOG1.Flags= cdlCCFullOpen
COMMONDIALOG1.ShowColor
RICHTEXTBOX1.SelColor= COMMONDIALOG1.color
Font dialog
The Fontdialogbox allowsthe usertoselecta fontby itssize,color,andstyle.
Once the usermakesselectionsinthe Fontdialogbox,the followingpropertiescontaininformation
aboutthe user'sselections.
Color The selectedcolor.
FontBoldReturnswhetherthe boldcheckbox wasselected.
FontItalic Returnswhetherthe italiccheckboxwasselected.
FontStrikethruReturnswhetherthe strikethroughcheckbox wasselected. Touse thisproperty,you
mustfirstset the FlagspropertytocdlCFEffects.
3. FontUnderline Returnswhetherthe underline checkboxwasselected.
FontName Returns the selectedfontname.
FontSize Returnsthe selectedfontsize.
Before displayingthe dialogTodisplaythe dialog,youmustfirstsetthe Flagspropertyeither
cdlCFScreenFontsorcdlCFPrinterFonts or cdlCFBoth,if youintendtoletthe userchoose between
bothscreenfontsand printerfonts
code
COMMONDIALOG1.Flags= cdlCFBoth+ cdlCFEffects
COMMONDIALOG1.ShowFont
RICHTEXTBOX1.SelFontName = COMMONDIALOG1.FontName
RICHTEXTBOX1.SelFontSize= COMMONDIALOG1.FontSize
RICHTEXTBOX1.SelBold= COMMONDIALOG1.FontBold
RICHTEXTBOX1.SelItalic= COMMONDIALOG1.FontItalic
RICHTEXTBOX1.SelStrikeThru= COMMONDIALOG1.FontStrikethru
Print dialog
The print dialogbox allowsthe usertoselecthow outputshouldbe printed.
Optionsandpropertiesaccessible tothe userincludesthe amountof copiestomake,printquality,
the range of pagesto print,etc.
CopiesThe amountof copiestoprint.
FromPage The startingpage of the printrange.
ToPage The endingpage of the printrange.
hDC The device contextforthe selectedprinter.
OrientationThe page'sorientation(asinportraitor landscape).
Before displayingthe dialogBefore showingthe dialog,feel freetosetthe appropriate printdialog
propertiestosetthe defaultvaluestouse.
Sample code
CommonDialog1.ShowPrinter
BeginPage =CommonDialog1.FromPage
4. EndPage = CommonDialog1.ToPage
NumCopies=CommonDialog1.Copies
Orientation=CommonDialog1.Orientation
RichTextbox Control
• The rich textbox control allowsthe usertoenterandedittext,providingmore advanced
formattingfeaturesthanthe conventional textboxcontrol. Youcan use differentfontsfordifferent
textsections.
You can evencontrol indents,hangingindents,andbulletedparagraphs. Thiscontrol isloadedby
selectingthe MicrosoftRichTextbox Control fromthe Componentsdialogbox.
Most of the properties,events,andmethodsassociatedwiththe conventionaltextbox are available
withthe rich textbox. A majordifference betweenthe twocontrolsisthatwiththe rich textbox,
multiple fontsizes,styles,andcolorsare supported.
Some unique propertiesof the rich textboxare:
FileName Canbe usedtoload the contentsof a .txt or .rtf file intothe control.
SelFontName Setthe fontname forthe selectedtext.
SelFontSize Setthe fontsize forthe selectedtext.
SelFontColorSetthe fontcolorfor the selectedtext.
selBold,selItalic,setselectedtexttobold,italic
SelStrikeThru
Some unique methodsof the richtextbox are:
LoadFile- Openafile andloadthe contentsintothe control.
SaveFile- Save the control contentsintoafile.