Wednesday, June 23, 2010

How to use Common dialog Control?
Hi to all, I am going to explain how to use a common dialog control, for that we must know what is common dialog control, and where is it what is the basic things
common dialog control intro
This tutorial will help in these things now I am going to explain other functions
In this tutorial I am going to use the following controls
I use a richttextbox to explain some properties and methods of a common dialog control
Name of the Richtextbox is Rtb
Name of the CommonDialogControl is Cmdlg
ShowOpen
This method is used to show a open dialog this dialog is normally use when you try to open a file
Cmdlg.ShowOpen
RichTextBox1.LoadFile (Cmdlg.FileName)

Now this code will open a dialog and when you choose a file name it will load in the Richtextbox

Quote

Note: This is an Application level model Dialog so the next line will execute after the closing of the dialog

Hold a second, you need open the dialog at a specified Directory (i.e.) Start up Directory,
so you want to set default opening directory, for that use the InitDir property
   Cmdlg.InitDir = "C:"

This will always open the C: as your default location try to change it whatever you want here
Now there are some unwanted file shows there like bmp, jpeg and so on. You want to avoid showing this type of files for user, because this type of files are not correctly opened by Richtextbox ,so you need to use the Filter Property
Filter property is used to show only Filtered Extensions now I am going to Filtered Textfiles (i.e) *.txt Files
   Cmdlg.Filter = "*.txt|*.txt|*.*|*.*"  

Wait. Now you ask a question why should you repeat it two times now run this code this will make you understand what it is ?
   Cmdlg.Filter = "TextFiles|*.txt|All Files|*.*"  

Yes, the first part is used to display to the user the second part is the filter extension, Note both are necessary, when you use a filter property
Now consider a situation the users cancel the dialog box , you have two ways to handle it
First one
   Cmdlg.Filter = "TextFiles|*.txt|All Files|*.*"
Cmdlg.FileName = ""
Cmdlg.InitDir = "C:"
Cmdlg.ShowOpen
If Cmdlg.FileName <> "" Then
Rtb.LoadFile (Cmdlg.FileName)
End If

If the filename property is empty then you should know that the user selects the cancel


Another way
On Error GoTo Er:
Cmdlg.Filter = "TextFiles|*.txt|All Files|*.*"
Cmdlg.CancelError = True
Cmdlg.ShowOpen
Rtb.LoadFile (Cmdlg.FileName)
Exit Sub
Er:
MsgBox Err.Description

Now if you Run this code, and click the cancel button in the dialog there will be a message box shown
“Cancel was selected”
The second way is the perfect way, you can use it in all the following dialogs
ShowSave
All things are similar to the open dialog
ShowFont
Now we are going to change the font of the rtb’s selection area
   Cmdlg.ShowFont
With Cmdlg
Rtb.SelFontName = .FontName
Rtb.SelBold = .FontBold
Rtb.SelItalic = .FontItalic
Rtb.SelFontSize = .FontSize
End With

Everybody knows that show font dialog will show the font dialog but when you run this piece of code you have an error no fonts installed did you know the reason why it says like that, because you (we) miss something (i.e.) we need to set the flags correctly
Consider the following code
   Cmdlg.Flags = cdlCFScreenFonts
Cmdlg.ShowFont
With Cmdlg
Rtb.SelFontName = .FontName
Rtb.SelBold = .FontBold
Rtb.SelItalic = .FontItalic
Rtb.SelFontSize = .FontSize
End With

Now a font dialog is perfectly opens and when we select it, it will affect the currently selected text of the richtextbox
Now even more using the flags property
   Cmdlg.Flags = cdlCFScreenFonts + cdlCFEffects
Cmdlg.ShowFont
With Cmdlg
Rtb.SelFontName = .FontName
Rtb.SelBold = .FontBold
Rtb.SelItalic = .FontItalic
Rtb.SelFontSize = .FontSize
Rtb.SelUnderline = .FontUnderline
Rtb.SelStrikeThru = .FontStrikethru
Rtb.SelColor = .Color
End With

See the difference you can see three more properties added here


now even more customized
 Private Sub cmdFont_Click()
On Error GoTo Er:
Cmdlg.Min = 10
Cmdlg.Max = 30
Cmdlg.Flags = cdlCFScreenFonts + cdlCFEffects + cdlCFLimitSize
Cmdlg.CancelError = True
Cmdlg.ShowFont
With Cmdlg
Rtb.SelFontName = .FontName
Rtb.SelBold = .FontBold
Rtb.SelItalic = .FontItalic
Rtb.SelFontSize = .FontSize
Rtb.SelUnderline = .FontUnderline
Rtb.SelStrikeThru = .FontStrikethru
Rtb.SelColor = .Color
End With
Exit Sub
Er:
MsgBox Err.Description
End Sub

Did you see any difference in your font dialog?
Yes, the fonts are in the limited size between the min and max range
These are some of the commonly used functions used by common dialog hope you enjoy it
Have a happy coding :D