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
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:
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.
Now we'll declare rslt as the DialogResult which will check which button on the FontDialog was pressed when it was displayed.
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:
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.
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
After declaring a new instance, we can modify the dialog to our specifications setting it's properties.Dim FD As New FontDialog
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
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.
FD.ShowColor = False
Code: Select all
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:
FD.ShowEffects = True
Code: Select all
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:
FD.ShowApply = True
Code: Select all
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.FD.MinSize = 12
FD.MaxSize = 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
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.
Dim rslt As DialogResult = FD.ShowDialog
Code: Select all
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:
If rslt = Windows.Forms.DialogResult.OK Then
Label1.Font = FD.Font
Label1.ForeColor = FD.Color
End If
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
Set the current color in the ColorDialog to the Label1's current ForeColor when the dialog is displayed:
Dim CD As New ColorDialog
Code: Select all
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:
CD.Color = Label1.ForeColor
Code: Select all
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?
CD.FullOpen = True
Code: Select all
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.If CD.ShowDialog = DialogResult.OK Then
Label1.ForeColor = CD.Color
End If
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.
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;

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.
The basics really are simple. What I did with this tutorial is include everything that was important and what the users actually use.
3 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023