Using FontDialog And ColorDialog

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Using FontDialog And ColorDialog
Usman55
In this tutorial, we'll cover the basics of using a FontDialog and ColorDialog.

First of all, add a Label and two Buttons to your Form. One of the button will be used to open the FontDialog and the other to open the ColorDialog.


Using a FontDialog:

A FontDialog is used to select the font of a control having some text associated to it. Double click on the first button and type in the following codes...

Create a new instance of a FontDialog:
Code: Select all
Dim FD As New FontDialog
After declaring a new instance, we can modify the dialog to our specifications setting it's properties.

The FontDialog by default shows about 10 colors from which we can choose the color of the font. We can disable it though, if we're going to use a ColorDialog or otherwise.
Code: Select all
FD.ShowColor = False
ShowEffects is another property which is set to True by default. It determines whether or not the effects like Bold, Italic etc. should be displayed on the dialog.
Code: Select all
FD.ShowEffects = True
If you want to apply the font to the text before closing the dialog, then you can turn on the Apply button by this code:
Code: Select all
FD.ShowApply = True
Other properties include the minimum and maximum size that the user can select for the font of the text. These can be used as follows:
Code: Select all
FD.MinSize = 12
        FD.MaxSize = 40
This code wouldn't allows the user to choose a size below 12 and above 40. It can be changed by changing the integers 12 and 40.

Now we'll declare rslt as the DialogResult which will check which button on the FontDialog was pressed when it was displayed.
Code: Select all
Dim rslt As DialogResult = FD.ShowDialog
If the OK button was pressed, then the font and color (if it wasn't disabled) of the text will be set to the FontDialog's settings.
Code: Select all
If rslt = Windows.Forms.DialogResult.OK Then
            Label1.Font = FD.Font
            Label1.ForeColor = FD.Color
        End If
Now one more thing I'd like to add here is that when the FontDialog opens, it's font, color etc. properties shouldn't be set to the default values but rather to the Label1's properties. We can accomplish that by typing this code after declaring the new instance of FontDialog:
Code: Select all
FD.Font = Label1.Font
        FD.Color = Label1.ForeColor

Using a ColorDialog:

A ColorDialog is used to set the Color properties of various controls like BackColor, ForeColor and any other kind of color. :P Double click on the second button and type the following codes...

To create a new instance of a ColorDialog:
Code: Select all
Dim CD As New ColorDialog
Set the current color in the ColorDialog to the Label1's current ForeColor when the dialog is displayed:
Code: Select all
CD.Color = Label1.ForeColor
Determine whether or not the extended color palette should be already open when the dialog is opened by setting this property to True or False:
Code: Select all
CD.FullOpen = True
Now to display the dialog we're going to use a different approach unlike what we used in the case of FontDialog. Both methods work both way but it's good to try out new things, isn't it?
Code: Select all
If CD.ShowDialog = DialogResult.OK Then
            Label1.ForeColor = CD.Color
        End If
This code says that if the OK button is pressed when the ColorDialog is displayed, then set the Label1's ForeColor to the color selected on the dialog.

ColorDialog is relatively easy to use as it is associated with 1 property only and that too has a single value unlike FontDialog in which the Font property consists of various values.
Image
User avatar
jordy03
New Member
New Member
Posts: 21
Joined: Thu Mar 06, 2014 6:09 pm

I thought it will be a simple one, really so advanced and you covered things that i've never came across, so proud of you Usman55 you deserved the reward of MM :) loove;
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Thanks #jordy03

The basics really are simple. What I did with this tutorial is include everything that was important and what the users actually use.
Image
3 posts Page 1 of 1
Return to “Tutorials”