» Home
» Tutorials
» Search  
» Contact us 

Home » Tutorials » Introduction to Visual Basic .NET » Chapter 7

Chapter 7: Displaying Dialog Boxes
7.5: The ColorDialog Control
7.5.2: Using the ColorDialog Control

Using the ColorDialog Control

All you need to do to display the Color Dialog box is to execute its ShowDialog method:

ColorDialog1.ShowDialog()

The ColorDialog control will return a DialogResult of OK or Cancel. Hence, you can use the previous statement in an If . . . End If statement and test for a DialogResult of OK, as you have done in the previous examples that you have coded.

To retrieve the color that the user has chosen, you simply retrieve the value set in the Color property and assign it to a variable or any property of a control that supports colors, such as the ForeColor property of a text box:

txtFile.ForeColor = ColorDialog1.Color

In this Exercise, you continue using the same project and make the ColorDialog control display the Color dialog box. Then, if the dialog box returns a DialogResult of OK, you change the background color of the form.

 

Exercise - Working with the ColorDialog Control

1. Switch to the Forms Designer in the Dialogs project.

2. On the form, add another Button control from the Toolbox and set its properties according to the values shown:

  • Set Name to btnColor.

  • Set Anchor to Top, Right.

  • Set Location to 367, 98.

  • Set Text to Color.

 

3. Next, add a ColorDialog control to your project from the Toolbox. It will be added to the workspace below the form, and you will accept all default properties for this control.

4. Double-click the Color button to bring up its Click event handler and add the following highlighted code:

Private Sub btnColor_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnColor.Click
‘Show the Color dialog
If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    ‘Set the BackColor property of the form
    Me.BackColor = ColorDialog1.Color
End If
End Sub

5. That’s all the code you need to add. To test your changes to this project, click the Start button.

6. Once the form is displayed, click the Color button to display the Color dialog box. Choose any color that you want, or create a custom color by clicking the Define Custom Colors button. Once you have chosen a color, click the OK button in the Color dialog box.

7. The background color of the form will be set to the color that you chose and the background color of the buttons will inherit the background color of the form.

8. As with the Font dialog box, you do not have to set the Color property of the ColorDialog control before displaying the Color dialog box again. It automatically remembers the color chosen, and this will be the color that is selected when the dialog box is displayed again. To test this, click the Color button again, and the color that you chose will be selected.

 

click next Page - How It Works.

Home | Link to Us | Partner Links | About us | Contact us

Copyright © 2009-2012 F1tutorials.com | All Rights Reserved