• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 824
  • Last Modified:

System.Drawing.Color - getting zero a lot from ToArgb()

System.Drawing.Color - getting zero a lot from ToArgb()

I seem to be getting the wrong value back from ToArgb().

I am setting the background colour of a TextBox using whatever colour is typed into the box.

eg: You type "red" into the box and click go, and the box's background turns red.

I am also displaying the ARGB value of the new colour in another textbox as an integer.

This works great for several colours, but when I use html colours like "FFFFFF" or "336699" I always get zero

for the ARGB. The colour is of course recognised and the box turns the correct colour. But the ARGB value

comes out zero.


'You can type an integer into one box or a name/Html RGB value into the other and
'the one you just entered will be stored as a System.Drawing.Color and the box
'is set to that colour. The one you DIDNT just enter is changed to match the new
'System.Drawing.Color, but in the integer (ARGB) box it comes out with a zero when it shouldn't.

Private Sub updateColourValues(ByRef sender As TextBox)
      Dim newColour As Color

      Select Case sender.ID
            Case "txtColourInteger"
                  newColour = System.Drawing.Color.FromArgb(CType(txtColourInteger.Text, Integer))
            Case "txtColourRgb"
                  newColour = System.Drawing.Color.FromName(txtColourRgb.Text)
      End Select

      txtColourInteger.Text = newColour.ToArgb()
      txtColourRgb.Text = newColour.Name

      txtColourInteger.BackColor = newColour
      txtColourRgb.BackColor = newColour
End Sub


This line:
txtColourInteger.Text = newColour.ToArgb()
is returning 0 instead of an integer representing the newColour's underlying ARGB value.

Any ideas?
1 Solution
Hi, I think txtColourInteger.Text = color.ToArgb(newColour.A,newColour.R,newColour.G,newColour.B).toString
M0m0ngaAuthor Commented:

ToArgb() can't take arguments:

ToArgb(anything) is wrong.

I think color.toargb() is wrong too.

(ToArgb() is a common method, its in the .NET BCL under System.Drawing.Color)

Anyone else have any ideas?
You know the ARGB value is an 32 bit integer with
A : Alpha component value,
R  :Red value
G :Green value
B : Blue value
And with a newcolor with type color like above, you can extract the AA,RR,BB,GG value out.
color.ToArgb(newColour.A,newColour.R,newColour.G,newColour.B).toString works on mine.
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

M0m0ngaAuthor Commented:
I must be missing something.

color.ToArgb(newColour.A,newColour.R,newColour.G,newColour.B).toString works on mine.

Did you build and run code with that exact line it it? Are you sure you don't mean FROMArgb or something?
Yes after read your post, i build up a test like this :
a textbox for user input , id:color1
a textbox for display result , id:color2
a check button
Private Sub check_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles check.Click
    Dim thecolor As Color = Color.FromName(color1.text)
    If thecolor.IsNamedColor Then
    color2.Text = Color.FromArgb(thecolor.A, thecolor.R, thecolor.G, thecolor.B).ToString
   color2.Text = "Not a valid color"
End Sub
M0m0ngaAuthor Commented:

This still does not address the question. It returns a value like this:
"Color [A=255, R=0, G=0, B=255]"
not the integer that the .NET framework uses to identify colours. As I originally said, I needed the underlying ARGB value, which will be one integer for each colour, not four integers. Of course I can already get the seperate A value, R value, G value and B value.

If you cannot answer the question, please just say so, don't pretend to understand.
Bob LearnedCommented:
You could probably get something from the ColorTranslator class.

M0m0ngaAuthor Commented:
Yes, I tried the ColorTranslator class.

In the end I solved my own problem by not using the value I wanted (the integer that the color namespace uses to represent a colour). I'm now storing all my colours as strings as HTML style RGB values eg: "#663399".

But I'll still give points to anyone who can explain the problem I was having or show me it's a documented bug or something.

But if that never happens, moderators please class this question as "solved it myself" and refund points (unless there are any legitimate objections). Thanks.
PAQed with points refunded (500)

Community Support Moderator

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now