Results 1 to 4 of 4

Thread: [RESOLVED] Problems loading .backcolor using color codes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Resolved [RESOLVED] Problems loading .backcolor using color codes

    I've some pictureboxes where I select the color from a colordialog and then I save the color in a settings.ini file.
    When I load the program, the instruction Form1_Shown loads the colors from the settings.ini file.

    When I save the colors to the ini file I transform then from System.Drawing.Color to a String (hex).
    The problem when loading the colors is that some colors are loaded correctly and others no. The other pictureboxes controls don't change the color.

    System.Drawing.Color to Hex
    Code:
    Private Function ColorConversion(colorObj As System.Drawing.Color) As String
            Return "#" & Hex(colorObj.R) & Hex(colorObj.G) & Hex(colorObj.B)
    End Function
    Hex to System.Drawing.Color (for loading the colors in the picboxes)
    Code:
    PictureBox1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF")
    in the ini file I've seen that some color codes in hex are less than 6 characters long:
    #FF00, #0FF0

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problems loading .backcolor using color codes

    Don't use Hex. You're using VB.NET so use VB.NET:
    Code:
    Private Function ColorConversion(colorObj As System.Drawing.Color) As String
        Return String.Format("#{0:X2}{1:X2}{2:X2}", colorObj.R, colorObj.G, colorObj.B)
    End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problems loading .backcolor using color codes

    Why bother with hex? Color has methods for converting to and from integers.
    Example
    Code:
            Dim foo As Color = Color.MistyRose
            'convert to string for saving
            Dim fooAsString As String = foo.ToArgb.ToString
    
            'convert string back to color from stored integer
            foo = Color.FromArgb(Integer.Parse(fooAsString))
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: Problems loading .backcolor using color codes

    Works good, thanks.

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