+ Reply to Thread
Results 1 to 6 of 6

Thread: Colors in VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Colors in VB.NET

    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:

    [highlight=VB.NET] Dim C As Color[/highlight]






    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:

    [highlight=VB.NET] Dim C As Color
    C = Color.Red
    C = Color.Tomato
    C = Color.Black
    C = Color.WhiteSmoke[/highlight]


    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.FromName("Blue")
    C = Color.FromName(myStringColorVariable)[/highlight]



    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.FromKnownColor(KnownColor.Brown)
    C = Color.FromKnownColor(KnownColor.ActiveCaptionText)
    C = Color.FromKnownColor(KnownColor.WindowText)[/highlight]



    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:


    [highlight=VB.NET] C = Color.FromArgb(30, 155, 0, 144)
    C = Color.FromArgb(255, 255, 0)
    C = Color.FromArgb(0, 0, 0)[/highlight]





    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.FromArgb(255, 255, 255, 255)
    MessageBox.Show(C.ToString)[/highlight]

    will output:


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

    While this:

    [highlight=VB.NET] Dim C As Color
    C = Color.LightGoldenrodYellow
    MessageBox.Show(C.ToString)[/highlight]

    will output this:


    Code:
    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.Aqua
    MessageBox.Show(C.ToKnownColor)[/highlight]


    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.Aqua
    MessageBox.Show(C.ToKnownColor.ToString)[/highlight]


    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:


    [highlight=VB.NET] 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")
    Else
    MessageBox.Show(Known.ToString)
    End If[/highlight]







    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.Chartreuse

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


    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:


    [highlight=VB.NET] Dim C As Color
    C = Color.Chartreuse
    Me.BackColor = C[/highlight]




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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Colors in VB.NET

    Neat! +Rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,525
    Blog Entries
    75
    Rep Power
    144

    Re: Colors in VB.NET

    Nicely done. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: Colors in VB.NET

    Well done! I might use in my next application!

  6. #5
    arkanion is offline Newbie
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Re: Colors in VB.NET

    nice topic

  7. #6
    Join Date
    Sep 2010
    Location
    Yangon, Myanmar
    Posts
    7
    Rep Power
    0

    Re: Colors in VB.NET

    nice topic

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. RGB to hex colors and hex colors to RGB - PHP
    By Vswe in forum PHP Tutorials
    Replies: 8
    Last Post: 10-06-2010, 10:17 AM
  2. Convert 256 colors to 16 colors - BMP Image
    By spyrytus in forum C and C++
    Replies: 9
    Last Post: 12-10-2009, 03:32 AM
  3. Command Colors
    By erik in forum Linux Applications
    Replies: 1
    Last Post: 08-07-2008, 05:52 AM
  4. Colors
    By NeedHelp in forum The Lounge
    Replies: 22
    Last Post: 07-11-2007, 09:49 PM
  5. Have you used colors?
    By Sir_Rimo in forum General Programming
    Replies: 2
    Last Post: 01-16-2007, 06:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts