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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.