+ Reply to Thread
Results 1 to 5 of 5

Thread: Colors in VB.NET

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,787
    Blog Entries
    5

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Colors in VB.NET

    Neat! +Rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,680
    Blog Entries
    57

    Re: Colors in VB.NET

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

  4. #4
    Learning Programmer Bioshox is on a distinguished road
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    88

    Re: Colors in VB.NET

    Well done! I might use in my next application!

  5. #5
    Newbie arkanion is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    10

    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. Replies: 8
    Last Post: 01-19-2010, 08:33 PM
  2. Replies: 3
    Last Post: 11-02-2009, 07:20 PM
  3. Replies: 3
    Last Post: 11-02-2009, 07:18 PM
  4. Replies: 1
    Last Post: 11-02-2009, 07:43 AM
  5. Replies: 1
    Last Post: 11-02-2009, 07:38 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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