How to use Color Dialog in VB.Net
February 19, 2011 Leave a comment
What is Color Dialog ?
How we can use it ?
There are two ways to create this component. First, drag and drop the color dialog from toolbox, you can find it under the tab of dialogs. After drag and drop name it properly because it will be accessible throughout the project as a global object. Second, you can directly create it as a class object in your code. Its scope will be limited to the code where it is created. It is a good practice to dispose the object when it is no more needed.
Code below shows how you invoke the object when you drag and drop the component from toolbox.
'Use Color Dialog added on this form from toolbox If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Me.BackColor = ColorDialog1.Color End If
and when you create it as a class object.
'creating color dialog component as class object Dim ColDialog As New ColorDialog If ColDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Me.BackColor = ColDialog.Color End If 'disposing class object ColDialog.Dispose()
Color dialog has very few commonly used properties, methods and events which controls the behaviour and appearance, lets see what are these and how we can use them. Below are two commonly used properties.
- AllowFullOpen
- FullOpen
- CustomColors
Dim ColDialog As New ColorDialog 'Here Checkbox Controls the option True/False ColDialog.AllowFullOpen = CheckBox1.Checked ColDialog.FullOpen = CheckBox2.Checked If ColDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Me.BackColor = ColDialog.Color End If ColDialog.Dispose()
The AllowFullOpen property controls the access of user from generating its own custom colors by restricting his/her access. It enables or disables the “Define Custom Colors” button. which controls the expansion of color dialog.
The second property FullOpen defines the default behaviour of color dialog. by setting this property user can see the expanded or contract form of dialog.
Color dialog has one important property that defines the custom colors. See the code below, which demonstrate the use of CustomColors property.
The CustomColor property accepts the integer array of max sixteen element, it is because color dialog has only sixteen custom color palate boxes, which can be customized. You can define the integer array by any method, however I have used hex and integer values in mixed for demonstration purposes. it is 24 Bit integer number ranging from 0 to 16777215 and similarly in hex from &H000000 to &HFFFFFF. This is RGB color scheme.
Dim ColDialog As New ColorDialog 'integer value range from 0 to 16777215 which is 24 bit integer value ColDialog.CustomColors = New Integer() {&H0, &HF12345, &H1F2345, &H12F345, _ 3758726, 12566463, 7526079, 7405793, _ 6945974, 241502, 2296476, 5130294, _ 3102017, 7324121, 14993507, &HF0FF0F} If ColDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Me.BackColor = ColDialog.Color End If ColDialog.Dispose()
Finally it has one event HelpRequest, to use this event, first you have to enable the help button on the dialog. it can be done by setting the ShowHelp property with True value. When user will click on the help button on dialog. This event will fire, you can place your help code in this event. see code snippet below.
Private Sub ColorDialog1_HelpRequest(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles ColorDialog1.HelpRequest 'place your help system code here MsgBox("You can display your help here.") End Sub
That’s It. Hope you have found this blog usefull. Please don’t forget to rate this. Download Project Source
Verification Code 5UZ7MDBQ94MJ