Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Colors in VB.NET

VB.NET form

  • Please log in to reply
7 replies to this topic

#1 Vswe


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 13 November 2009 - 04:25 PM

I will in this tutorial teach you about how to use Colors in VB.NET, colors can then be used to changed the color of object etc.(not so hard to guess). In VB.NET it exists a data type called color, this is what we will be using. To declare a color variable we simply to it like this:

        Dim C As Color

Creating Colors

But there's a lot of ways of creating a color, one way is using one of the colors that already exists, it could look something like this:

        Dim C As Color
C = Color.Red
C = Color.Tomato
C = Color.Black
C = Color.WhiteSmoke

You probably get the idea. If you only have a string value of one of all the colors you can also create a color, however you must use Color.FromName(), an example is showed below:

        Dim C As Color
C = Color.FromName("Blue")
C = Color.FromName(myStringColorVariable)

You can also use the KnownColors, this will work a bit like the first way but here you can also get some special colors, like the WindowText color which is the color of the text used in client areas in Windows. Here's follows an example:

       Dim C As Color
C = Color.FromKnownColor(KnownColor.Brown)
C = Color.FromKnownColor(KnownColor.ActiveCaptionText)
C = Color.FromKnownColor(KnownColor.WindowText)

Another way which is very useful is using FromArgb, when using this you'll use Integers to specify the alpha, the red, the green and the blue in a color to create it. All these values should be between 0 and 255. You don't have to use alpha if you don't want to. Here follows some examples:

        C = Color.FromArgb(30, 155, 0, 144)
C = Color.FromArgb(255, 255, 0)
C = Color.FromArgb(0, 0, 0)

Getting info about Colors

If we already have a color we could get some info about it, sometimes it could be useful to just show some info about it, use it in conditions for checking if the colors is "good" or when creating new colors. Firstly if you only want some generally info you can use ToString, however this is not the best thing to use since it will output a name or a Argb value depending on what you used when creating the color. It's better to use something which always returns the same type. However it could be good for debugging. Here comes two examples:

        Dim C As Color
C = Color.FromArgb(255, 255, 255, 255)

will output:

Color [A=255, R=255, G=255, B=255]

While this:

        Dim C As Color
C = Color.LightGoldenrodYellow

will output this:

Color [LightGoldenrodYellow]

However, you can use ToKnownColor to get the name of the Known color(if it is a know color, else it will return 0). To use it you'll need to do something like this:

        Dim C As Color
C = Color.Aqua

The problem now is that it only gives us the index of the KnowColor's Enum member. To solve this we will have to use ToString on ToKnownColor, like this:

        Dim C As Color
C = Color.Aqua

But since there's no Enum member at index 0 called "Not a known color" or something similar we will still just get "0" if the color isn't a known color, to solve this we can simply add an if statement:

        Dim C As Color
C = Color.FromArgb(1, 2, 3, 4)

Dim Known As KnownColor
Known = C.ToKnownColor

If Known = 0 Then
MessageBox.Show("That color is not a known color")
End If

We can also get the Argb value by using ToArgb, but the integer value you'll get is pretty hard to get any information from I recommend you to get The Alpha value, the Red value, the Green value and the blue value one by one by using A, R, G and B, like so:

        Dim C As Color
C = Color.Chartreuse

MessageBox.Show("Alpha = " & C.A & " Red = " & C.R & " Green = " & C.G & " Blue = " & C.B)

This could be good when you easy want to get the color's construction.

I haven't really showed how to use a color then but I think you've understood that it works like a normal variable and therefor you can just do like this:

        Dim C As Color
C = Color.Chartreuse
Me.BackColor = C

And that was pretty much what I wanted to teach you in this tutorial. Have a fun time coding, bye. :)
  • 2

#2 Guest_Jordan_*

  • Guest

Posted 14 November 2009 - 05:36 AM

Neat! +Rep
  • 0

#3 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 14 November 2009 - 02:22 PM

Nicely done. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

#4 Bioshox


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 15 December 2009 - 02:50 PM

Well done! I might use in my next application!
  • 0

#5 arkanion


    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 13 January 2010 - 12:01 PM

nice topic
  • 0

#6 pyaephyokyaw1992


    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 23 September 2010 - 09:46 PM

nice topic
  • 0

#7 zeroGiven


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 61 posts
  • Programming Language:Visual Basic .NET
  • Learning:C#, Visual Basic .NET

Posted 28 February 2013 - 12:37 PM

Also, don't forget about SystemColors


For example, if you look at the default backcolor of a form it is set to Control or even the ForeColor is set to ControlText


To get to these colors you would use



Me.BackColor = SystemColors.Control
Me.ForeColor = SystemColors.ControlText

  • 0

#8 VinodKc


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 12 June 2015 - 06:09 AM

Thank you. Nice tutorial. Nice presentation. Splitting a complex code into lines and describe each line's important. This is actually a good way to teach a complex subject very easily. Thanks a lot :)

Edited by VinodKc, 12 June 2015 - 06:09 AM.

  • 0