Results 1 to 4 of 4

Thread: Convert color to alternate data type and back again

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    12

    Convert color to alternate data type and back again

    I need to convert a color from a ColorDialog to an appropriate data type and run it back into the program on the form load. Now it works good on selecting the color and loading any settings but the color will not load. I've looked around the internet but none of the threads I see help. Here's what I have so far:

    Public C As Integer
    Code:
            Dim cDialog As New ColorDialog()
            cDialog.FullOpen = True
            cDialog.Color = frmEx.BackColor
            If (cDialog.ShowDialog() = DialogResult.OK) Then
                frmEx.BackColor = cDialog.Color
                C = cDialog.Color.ToArgb
            End If
    This returns an example color as: -12457079

    I need to be able to convert this back to a color but have had no luck in doing so. Can anyone help?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Convert color to alternate data type and back again

    What is C declared as? It should be a UInt for unsigned integer.

    You used ToArgb to get the integer, so you should use FromArgb to get the color back from the integer.

    It actually looks like C can be just a plain Integer, and as long as you are not bit-shifting with that value, that does seem reasonable. I think that a UInt makes a bit more sense, since the integer is really just holding the four byte values and isn't truly ever a negative number, but it doesn't appear to make much difference.
    My usual boring signature: Nothing

  3. #3
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Convert color to alternate data type and back again

    Luck has nothing to do with it.

    The color class contains a method to convert an Argb value into a color.

    Ten seconds to type "vb.net argb to color" into Google and the first hits you get tell you how to do it.

    Dim ArgbValue as integer = (Color.Blue).ToArgb
    Dim oColor as Color = Color.FromArgb(ArgbValue)

    [edit]
    Beg pardon Shaggy.
    [/edit]
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    12

    Re: Convert color to alternate data type and back again

    It's always something simple that I over think. Thank you both for the help and Shaggy, C was defined as an integer just for the record. Thanks again!

Posting Permissions

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



Click Here to Expand Forum to Full Width