Support Forums

Full Version: [TUT] Colors [VB]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]






[SIZE="6"]Creating Colors[/SIZE]



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]





[SIZE="6"]Getting info about Colors[/SIZE]

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]
Nice tutorial but it's ripped :/
Obviously ripped. Not even an attempt to cover it up.