Mohammad Aldefrawy
Mohammad Aldefrawy

Reputation: 563

Color.fromArgb dosen't accept integer

In visual basic 2015 the function Color.fromArgb can't accept the integer of the function Color.toArgb it can accept only the following:

FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color

I save an integer from function Color.toArgb from a color dialoge to database. When i try to load and use it by the function Color.fromArgb it doesn't accept the integer as a parameter.

ColorDialog1.ShowDialog()
TextBox1.Text = ColorDialog1.Color.ToArgb
TextBox1.ForeColor = ColorDialog1.Color

then i recall it

TextBox2.ForeColor = Color.FromArgb(TextBox1.Text)

it gives error.

Is there a simple way to solve this?

Upvotes: 0

Views: 3287

Answers (4)

Josip Sibenik
Josip Sibenik

Reputation: 21

If it does not work remove the alpha parameter

Public Shared Function IntegerToColor(ByRef RGB As Int32) As Color
    Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
    'Dim Alpha As Byte = Bytes(3)
    Dim Red As Byte = Bytes(2)
    Dim Green As Byte = Bytes(1)
    Dim Blue As Byte = Bytes(0)
    'Return Color.FromArgb(Alpha, Red, Green, Blue)
    Return Color.FromArgb(Red, Green, Blue)
End Function

Upvotes: 0

Mohammad Aldefrawy
Mohammad Aldefrawy

Reputation: 563

I solved this by using a function that converts the integer from Color.FromArgb

Function IntegerToColor(ByRef RGB As Int32) As Color
    Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
    Dim Alpha As Byte = Bytes(3)
    Dim Red As Byte = Bytes(2)
    Dim Green As Byte = Bytes(1)
    Dim Blue As Byte = Bytes(0)
    Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function

I coped it from another answer and it worked with me

Upvotes: 0

AJ X.
AJ X.

Reputation: 2770

TextBox1.Text is going to return you a string. First convert it to an integer. See this MSDN article for details:

Dim number As Integer = Int32.Parse(TextBox1.Text)

Then pass the number into FromArgb():

FromArgb() is overloaded. According to MSDN, you can pass in integers as well:

SolidBrush red = new SolidBrush(Color.FromArgb(120, 255, 0, 0));

If you're looking for a simpler option you could do something like:

int transparency = 75;
var transparentColor = Color.FromArgb(transparency, Color.Red);

Upvotes: 1

iamLinker
iamLinker

Reputation: 98

Here's a code sample that supports Plutonix's answer

Color.FromArgb(CInt(red_txt.Text), CInt(green_txt.Text), CInt(blue_txt.Text))

Upvotes: 0

Related Questions