Use the colors of your program in Visual Studio

This example arose from a query in this article .

The control ColorDialog presents a traditional color palette, but the truth is that visual studio is much nicer and shows all the colors by name as defined in the System.Drawing object properties.

Given the problem that there is no control to show us this list of colors we see how we can constriur one ourselves.

For example we need a new project and two forms.

In the first form a baton just have to call the Form 2, as shown in the following figure:

coloresNombre001

button and the code is as follows:

 BtnColor_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
 As New Form2 Dim vForm2
 vForm2.ShowDialog ()
 End Sub

and in this form will n botoes ListBox and two (accept and cancel) as in the following form:

coloresNombre002

In the form's Load event establish some properties of the ListBox and carry the names of the colors with the following code:

 Form2_Load Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ListBox1.DrawMode = DrawMode.OwnerDrawFixed
 ListBox1.ItemHeight = 20

 ListBox1.Items.Clear ()
 ListBox1.BeginUpdate ()
 For Each pi As Reflection.PropertyInfo In GetType (Color). GetProperties (Or Reflection.BindingFlags.Static Reflection.BindingFlags.Public)
 ListBox1.Items.Add (pi.Name)
 Next
 ListBox1.EndUpdate ()
 End Sub

In the first line of Load "ListBox1.DrawMode = DrawMode.OwnerDrawFixed" establish that all elements of control are drawn manually and have the same height. This ademças, DrawItem event triggers a show just before each item.

To set the DrawItem event of ListBox, select (in design view) the ListBox1, we press F4 to view the properties, and the top of the Properties box select the lightning bolt to see the event that the control is available , we DrawItem and we double-click. The following image illustrates how:

coloresNombre003

and this is the code of DrawItem event

 ListBox1_DrawItem Private Sub (ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
 Dim rectangle As Rectangle = e.Bounds

 If (e.State And DrawItemState.Selected) Then
 e.Graphics.FillRectangle (SystemBrushes.Highlight, rectangle)
 Else
 e.Graphics.FillRectangle (Brushes.White, rectangle)
 End If

 Dim As String = ListBox1.Items nombreColor (e.Index)

 Dim b As New SolidBrush (Color.FromName (nombreColor))

 rectangulo.Inflate (-16, -2)
 e.Graphics.FillRectangle (b, New Rectangle (rectangulo.X, rectangulo.Y, 30, rectangulo.Height))

 e.Graphics.DrawRectangle (Pens.Black, New Rectangle (rectangulo.X, rectangulo.Y, 30, rectangulo.Height))

 If (e.State And DrawItemState.Selected) Then
 e.Graphics.DrawString (nombreColor, e.Font, Brushes.White, rectangulo.X + 34, rectangulo.Y + 2)
 Else
 e.Graphics.DrawString (nombreColor, e.Font, Brushes.Black, rectangulo.X + 34, rectangulo.Y + 2)
 End If
 End Sub

Now we just need to program the buttons eventosde as shown in the following code:

 BtnAceptar_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
 My.Forms.Form1.BackColor = Color.FromName (ListBox1.SelectedItem)
 End Sub

 BtnCancelar_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
 Me.Close ()
 End Sub

Well, to test the application ... if all went well, looks like this:

coloresNombre004

I leave some definitions:

Graphics.FillRectangle (Method): Fills the interior of a rectangle specified by a coordinate pair, a width and a height.

Graphics.DrawRectangle (Method): Draw a rectangle specified by a coordinate pair, a width and a height.

Graphics.DrawString (Method): Draw the specified text string at the specified location and with the specified Brush and Font objects.

Hope this helps and you can use in your projects.

11 Responses to "Use Visual Studio colors in your program"

  1. Information Bitacoras.com ...

    Rate in Bitacoras.com: This example arose from a query in this article. The control ColorDialog tresenta us a traditional color palette, but it is tru e that of visual studio is much nicer and shows all the colors for your n .....

  2. Cristyan says:

    Very good example, thank you.

  3. Jose Luis says:

    GREAT!
    Is greatly appreciated!

  4. Oscar Daniel says:

    ! Very good!
    the only thing that I worked was the cancel button.

  5. Erick says:

    fails to select the transparent color, it will be? ........... the other colors if it works

    salu2

    • Elisha says:

      is that the transparent color can not be applied as the background color of a form, but if applied to other controls.

      Thanks for commenting!

  6. gercer89 says:

    saved my life this example! ... but as I get to see the prpiedades screen at which you change the color? because it lets me see "My.Forms.Form1.BackColor." thanks salu2

  7. diego says:

    hello
    I get an error Expected identifier exactly. <A If (e.State And DrawItemState. Select ed) Then
    e.Graphics.FillRectangle (SystemBrushes.Highlight, rectangle)

    HELP ME BY FA
    Graciass

  8. Henry says:

    Greetings ...
    I'm looking for techniques to make a nicer design of controls that must be like the airport but I have no idea nor could I find something please if you could help me with that of anetmano thank you very much ...

  9. David says:

    this VERA, thank you very much for this example. Will I be able to apply it to my needs

    Thank you from Merida, Yucatan

Comment