Results 1 to 8 of 8

Thread: Color saves differently?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Color saves differently?

    When I save the background color of a textbox to a file, the code that saves is different.

    For example; A TextBox has this color = &H89E295. If I save it to a file, using WriteData(1).Clr = Text1.BackColor, it saves as 9036437.

    In some cases, when I open the file, it will open as the same color. But not all colors open as original.

    Ideas??

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Color saves differently?

    Quote Originally Posted by meopilite View Post
    When I save the background color of a textbox to a file, the code that saves is different.

    For example; A TextBox has this color = &H89E295. If I save it to a file, using WriteData(1).Clr = Text1.BackColor, it saves as 9036437.

    In some cases, when I open the file, it will open as the same color. But not all colors open as original.

    Ideas??
    &H89E295 = 9036437

    &H means Hexadecimal.

    Hexadecimal is base 16, whilst we normally use base 10 numbers.

    Base 10: Hexadecimal (Base 16) Binary (Base 2)
    1 1 1
    2 2 10
    3 3 11
    4 4 100
    5 5 101
    6 6 110
    7 7 111
    8 8 1000
    9 9 1001
    10 A 1010
    11 B 1011
    12 C 1100
    13 D 1101
    14 E 1110
    15 F 1111
    16 10 10000
    17 11 10001
    18 12 10010
    19 13 10011
    20 14 10100

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Re: Color saves differently?

    Thanks

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Color saves differently?

    To save color as hex
    Code:
    WriteData(1).Clr = Hex$(Text1.BackColor)
    To read color from hex
    Code:
    Text1.BackColor = Val("&H" & WriteData(1).Clr)



  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Color saves differently?

    Avoid the creaky old Val() and Str$() functions. These are legacy functions, mostly obsolete, and they have side effects such as being locale-blind that may trip you up. It is shocking to see people using them blindly.

    In place of the old Val() you should be using the newer strongly typed conversion functions such as CLng(), CSng() and so on.

    In place of the old Str$() you should use CStr() instead.

    In many cases you can get lucky and using the legacy conversions won't break your programs... until they do.


    What appears to be going on in this program is implicit conversion. We haven't been told what your WriteData(1).Clr is, but the Clr member appears to be type String. BackColor properties of VB controls are type Long.

    If so, the compiler is chuckling at you and silently inserting an implicit call to CStr() to automatically resolve your type mismatch.


    This might help you see this more clearly:

    Code:
    Option Explicit
    
    Private Type WriteData
        Clr As String 'This member is type String.
    End Type
    
    Private WriteData(1) As WriteData
    
    Private Sub Form_Load()
        AutoRedraw = True
        Font.Name = "Courier New"
        Font.Size = 10
        CurrentY = Text1.Height + Text1.Top * 2 'Move printing below Text1.
    
        Text1.BackColor = &H89E295
    
        WriteData(1).Clr = Text1.BackColor
        Print "WriteData(1).Clr =      Text1.BackColor  gives: """ & WriteData(1).Clr & """"
    
        WriteData(1).Clr = CStr(Text1.BackColor)
        Print "WriteData(1).Clr = CStr(Text1.BackColor) gives: """ & WriteData(1).Clr & """"
    
        WriteData(1).Clr = Str$(Text1.BackColor)
        Print "WriteData(1).Clr = Str$(Text1.BackColor) gives: """ & WriteData(1).Clr & """"
    
        WriteData(1).Clr = Hex$(Text1.BackColor)
        Print "WriteData(1).Clr = Hex$(Text1.BackColor) gives: """ & WriteData(1).Clr & """"
    End Sub
    Name:  sshot.png
Views: 89
Size:  2.3 KB

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Color saves differently?

    The compiler also finds this amusing because Val() returns a Double value:

    Code:
    Text1.BackColor = Val("&H" & WriteData(1).Clr)
    It silently compiles this as:

    Code:
    Text1.BackColor = CLng(Val("&H" & WriteData(1).Clr))
    What you want to use instead is:

    Code:
    Text1.BackColor = CLng("&H" & WriteData(1).Clr)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Re: Color saves differently?

    Things seem to be working by using this line;

    Code:
    If ReadData(1).Clr = 9036437 Then frmOne.Text1.BackColor = &H89E295
    Its kinda messy, but works.

    BTW Clr is defined in a module as;

    Code:
    Type Record
    
        Clr As String * 7
    
    End Type

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Color saves differently?

    It is just as easy to assign the decimal string. There is nothing magical about using a hex string.

    Just use:

    Code:
    Text1.BackColor = CLng(ReadData(1).Clr)
    Of course your Clr As String * 7 may be truncating values more than 7 digits long.

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