Results 1 to 18 of 18

Thread: [RESOLVED] Writing Colors to ini for later use

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Writing Colors to ini for later use

    I am wanting to write the color values to a ini file for later use. This is the code I am using and what the output is. Am I doing it right or would you recommend a different approach?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdBackgroundColor_Click()
    4.     ShowColorPicker
    5.     lblColorScheme.BackColor = CDlgColorPicker.Color
    6. End Sub
    7.  
    8. Private Sub cmdForegroundColor_Click()
    9.     ShowColorPicker
    10.     lblColorScheme.ForeColor = CDlgColorPicker.Color
    11. End Sub
    12.  
    13. Private Sub ShowColorPicker()
    14.     With CDlgColorPicker
    15.         .Flags = cdlCCFullOpen
    16.         .ShowColor
    17.         .CancelError = True
    18.     End With
    19. End Sub
    20.  
    21. Private Sub cmdSave_Click()
    22.     Dim intFF As Integer, strFileName As String, strFileLocation As String
    23.    
    24.     intFF = FreeFile
    25.    
    26.     strFileName = "ColorScheme.ini"
    27.     strFileLocation = App.Path & "\" & strFileName
    28.    
    29.     Open strFileLocation For Output As #intFF
    30.    
    31.     With lblColorScheme
    32.         Print #intFF, .BackColor & "|" & .ForeColor
    33.     End With
    34.    
    35.     Close #intFF
    36. End Sub
    output is:
    Code:
    12615808|255

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Writing Colors to ini for later use

    Why not store in the registry using SaveSetting and GetSetting ?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Writing Colors to ini for later use

    Quote Originally Posted by CVMichael
    Why not store in the registry using SaveSetting and GetSetting ?
    my client doesnt allow writing to the registry so i have to use text files instead

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Writing Colors to ini for later use

    Now that I actually looked at the code, I did not see anything wrong with it...

    Looks OK to me...

    What kind of problems to you have exactly ?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Writing Colors to ini for later use

    Quote Originally Posted by CVMichael
    Now that I actually looked at the code, I did not see anything wrong with it...

    Looks OK to me...

    What kind of problems to you have exactly ?
    when i retrieve the values from the ini file and then load the "colors" to the forecolor and backcolor properties. i am getting a type mismatch error.

    this is what i am using:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strParts() As String, sngSectionValue As Single, strLine As String
    3.     Dim sngBC As Single, sngFC As Single
    4.     Dim strFileName As String, strFileLocation As String, intFF As Integer
    5.    
    6.     strFileName = "ColorScheme.ini"
    7.     strFileLocation = App.Path & "\" & strFileName
    8.    
    9.     If FileExists(strFileLocation) Then
    10.         Open strFileLocation For Input As #intFF
    11.             Line Input #intFF, strLine
    12.                 strParts = Split(strLine, "|")
    13.                     For sngSectionValue = 0 To 1
    14.                         sngBC = strParts(0)
    15.                         sngFC = strParts(1)
    16.                     Next sngSectionValue
    17.                 Close #intFF
    18.     End If
    19. End Sub

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Writing Colors to ini for later use

    First of all, you don't need the For loop, I'm not sure why you put it in there...

    Try using the Val function to convert from string to number
    sngBC = Val(strParts(0))
    sngFC = Val(strParts(1))

    And second, a color is stored in a Long value type, not a Single type, so change the sngBC and sngFC variables to Long

  7. #7

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Writing Colors to ini for later use

    Bad file name or number
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strParts() As String, strLine As String
    3.     Dim lngBC As Long, lngFC As Long
    4.     Dim strFileName As String, strFileLocation As String, intFF As Integer
    5.    
    6.     strFileName = "ColorScheme.ini"
    7.     strFileLocation = App.Path & "\" & strFileName
    8.    
    9.     If FileExists(strFileLocation) Then
    10.         [hl]Open strFileLocation For Input As #intFF[/hl]
    11.             Line Input #intFF, strLine
    12.                 strParts = Split(strLine, "|")
    13.                         lngBC = Val(strParts(0))
    14.                         lngFC = Val(strParts(1))
    15.                 Close #intFF
    16.     End If
    17. End Sub

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Writing Colors to ini for later use

    i forgot to do intFF = FreeFile

    i am almost there, just one of the properties is being populated with the relevant color (backcolor)

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Writing Colors to ini for later use

    this is the content of the file:
    Code:
    0|16777215
    and when i do a breakpoint, both variables for the colors contain 0 instead of one containing 0 and the other containing 16777215
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strParts() As String, strLine As String
    3.     Dim lngBC As Long, lngFC As Long
    4.     Dim strFileName As String, strFileLocation As String, intFF As Integer
    5.    
    6.     intFF = FreeFile
    7.    
    8.     strFileName = "Settings.ini"
    9.     strFileLocation = App.Path & "\" & strFileName
    10.    
    11.     If FileExists(strFileLocation) Then
    12.         Open strFileLocation For Input As #intFF
    13.             Line Input #intFF, strLine
    14.                 strParts = Split(strLine, "|")
    15.                         lngBC = Val(strParts(0))
    16.                         lngFC = Val(strParts(1))
    17.                 Close #intFF
    18.     End If
    19.    
    20.     With lblColorScheme
    21.         .BackColor = lngBC
    22.         .ForeColor = lngFC
    23.     End With
    24. End Sub

  11. #11
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Writing Colors to ini for later use

    When writing to to Ini files, and getting info from them, I actually prefer using APIs:
    GetPrivateProfileString
    http://www.allapi.net/apilist/GetPri...leString.shtml

    WritePrivateProfileString
    http://www.allapi.net/apilist/WriteP...leString.shtml

    There are also various other Reading / Writing INI files APIs, which can make your life a bit easier :
    WriteProfileSection
    http://www.allapi.net/apilist/WriteP...eSection.shtml

    GetPrivateProfileSection
    http://www.allapi.net/apilist/GetPri...eSection.shtml

    GetPrivateProfileInt
    http://www.allapi.net/apilist/GetPri...ofileInt.shtml

    GetPrivatePrivateProfileSectionNames
    http://www.allapi.net/apilist/GetPri...ionNames.shtml

    to name a few
    VB.NET MVP 2008 - Present

  12. #12
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Writing Colors to ini for later use

    Agreed, HanneSThEGreaT. Why bother with splitting strings etc when a one liner will do? There's also the appalingly badly documented "Write/Get PrivateProfileStruct" APIs I mentioned in this thread. They don't seem to have caught on, yet . Maybe I should put something in the FAQs or codebank ?

    BTW, is it something I've done wrong, or have the VBCODE (and other) tags gone kaput?

    Try this:
    Code:
    Option Explicit
    
    'Put 2 command buttons and a label on a form.
    
    Private Declare Function WritePrivateProfileStruct Lib "kernel32.dll" Alias "WritePrivateProfileStructA" _
    (ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
    ByVal uSizeStruct As Long, ByVal szFile As String) As Long
    
    Private Declare Function GetPrivateProfileStruct Lib "kernel32.dll" Alias "GetPrivateProfileStructA" _
    (ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
    ByVal uSizeStruct As Long, ByVal szFile As String) As Long
    
    Private Type MyUDT
       Password As String * 20
       FormBackColour As Long
       LabelBackColour As Long
       LabelForeColour As Long
    End Type
    
    Private Sub Command1_Click() 'Change some colours etc...
       Label1.Caption = "Christmas"
       Form1.BackColor = vbRed
       Label1.BackColor = vbYellow
       Label1.ForeColor = vbBlue
    End Sub
    
    Private Sub Command2_Click() 'Save the settings...
       Dim MyNewUDT As MyUDT
    
       MyNewUDT.Password = Label1.Caption
       MyNewUDT.FormBackColour = Form1.BackColor
       MyNewUDT.LabelBackColour = Label1.BackColor
       MyNewUDT.LabelForeColour = Label1.ForeColor
       
       Call WritePrivateProfileStruct("MySection", "MySettings", _
          MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
    
    'File contents:
    '[MySection]
    'MySettings=4368726973746D61732020202020202020202020FF000000FFFF00000000FF000A
    End Sub
    
    Private Sub Form_Load() 'Load `em up...
       Dim MyNewUDT As MyUDT
          
       Call GetPrivateProfileStruct("MySection", "MySettings", _
          MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
    
       Label1.Caption = MyNewUDT.Password
       Form1.BackColor = MyNewUDT.FormBackColour
       Label1.BackColor = MyNewUDT.LabelBackColour
       Label1.ForeColor = MyNewUDT.LabelForeColour
    
    End Sub
    Last edited by schoolbusdriver; Nov 11th, 2006 at 06:24 AM.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Writing Colors to ini for later use

    yep. worked great. thanks for the help.

    Quote Originally Posted by schoolbusdriver
    Agreed, HanneSThEGreaT. Why bother with splitting strings etc when a one liner will do? There's also the appalingly badly documented "Write/Get PrivateProfileStruct" APIs I mentioned in this thread. They don't seem to have caught on, yet . Maybe I should put something in the FAQs or codebank ?

    BTW, is it something I've done wrong, or have the VBCODE (and other) tags gone kaput?

    Try this:
    Code:
    Option Explicit
    
    'Put 2 command buttons and a label on a form.
    
    Private Declare Function WritePrivateProfileStruct Lib "kernel32.dll" Alias "WritePrivateProfileStructA" _
    (ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
    ByVal uSizeStruct As Long, ByVal szFile As String) As Long
    
    Private Declare Function GetPrivateProfileStruct Lib "kernel32.dll" Alias "GetPrivateProfileStructA" _
    (ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
    ByVal uSizeStruct As Long, ByVal szFile As String) As Long
    
    Private Type MyUDT
       Password As String * 20
       FormBackColour As Long
       LabelBackColour As Long
       LabelForeColour As Long
    End Type
    
    Private Sub Command1_Click() 'Change some colours etc...
       Label1.Caption = "Christmas"
       Form1.BackColor = vbRed
       Label1.BackColor = vbYellow
       Label1.ForeColor = vbBlue
    End Sub
    
    Private Sub Command2_Click() 'Save the settings...
       Dim MyNewUDT As MyUDT
    
       MyNewUDT.Password = Label1.Caption
       MyNewUDT.FormBackColour = Form1.BackColor
       MyNewUDT.LabelBackColour = Label1.BackColor
       MyNewUDT.LabelForeColour = Label1.ForeColor
       
       Call WritePrivateProfileStruct("MySection", "MySettings", _
          MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
    
    'File contents:
    '[MySection]
    'MySettings=4368726973746D61732020202020202020202020FF000000FFFF00000000FF000A
    End Sub
    
    Private Sub Form_Load() 'Load `em up...
       Dim MyNewUDT As MyUDT
          
       Call GetPrivateProfileStruct("MySection", "MySettings", _
          MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
    
       Label1.Caption = MyNewUDT.Password
       Form1.BackColor = MyNewUDT.FormBackColour
       Label1.BackColor = MyNewUDT.LabelBackColour
       Label1.ForeColor = MyNewUDT.LabelForeColour
    
    End Sub

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: [RESOLVED] Writing Colors to ini for later use

    object variable or with block variable not set
    VB Code:
    1. Public Sub FormColorScheme(frm As Form)
    2.     Dim Ctrl As Control
    3.    
    4.     If FileExists(App.Path & "\Settings.ini") Then
    5.         Call GetPrivateProfileStruct("ColorScheme", _
    6.                                                    "ApplicationColorScheme", _
    7.                                                    ColorScheme, _
    8.                                                    Len(ColorScheme), _
    9.                                                    App.Path & "\" & "Settings.ini")
    10.        
    11.         frm.BackColor = ColorScheme.lngBC
    12.    
    13.         [hl]If TypeOf Ctrl Is Label Then[/hl]
    14.             With Ctrl
    15.                 .BackColor = ColorScheme.lngBC
    16.                 .ForeColor = ColorScheme.lngFC
    17.             End With
    18.         ElseIf TypeOf Ctrl Is ListView Then
    19.             With Ctrl
    20.                 .BackColor = ColorScheme.lngBC
    21.                 .ForeColor = ColorScheme.lngFC
    22.             End With
    23.         ElseIf TypeOf Ctrl Is Calendar Then
    24.             '
    25.         End If
    26.     End If
    27. End Sub

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: [RESOLVED] Writing Colors to ini for later use

    i forgot something in my code and was doing it by memory, duh! the below works
    VB Code:
    1. Public Sub FormColorScheme(frm As Form)
    2.     Dim Ctrl As Control
    3.    
    4.     If FileExists(App.Path & "\Settings.ini") Then
    5.         Call GetPrivateProfileStruct("ColorScheme", _
    6.                                                    "ApplicationColorScheme", _
    7.                                                    udtColorScheme, _
    8.                                                    Len(udtColorScheme), _
    9.                                                    App.Path & "\" & "Settings.ini")
    10.        
    11.         frm.BackColor = udtColorScheme.lngBC
    12.    
    13.         For Each Ctrl In frm.Controls
    14.             If TypeOf Ctrl Is Label Then
    15.                 With Ctrl
    16.                     .BackColor = udtColorScheme.lngBC
    17.                     .ForeColor = udtColorScheme.lngFC
    18.                 End With
    19.             ElseIf TypeOf Ctrl Is ListView Then
    20.                 With Ctrl
    21.                     .BackColor = udtColorScheme.lngBC
    22.                     .ForeColor = udtColorScheme.lngFC
    23.                 End With
    24.             ElseIf TypeOf Ctrl Is Calendar Then
    25.                 '
    26.             End If
    27.         Next Ctrl
    28.     End If
    29. End Sub

  16. #16
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: [RESOLVED] Writing Colors to ini for later use

    Good code schoolbusdriver!
    Unfortunately I can't add to your rep...
    VB.NET MVP 2008 - Present

  17. #17
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [RESOLVED] Writing Colors to ini for later use

    Np probs HanneSThEGreaT . Now if only BrailleSchool thinks of that...

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: [RESOLVED] Writing Colors to ini for later use

    Quote Originally Posted by schoolbusdriver
    Np probs HanneSThEGreaT . Now if only BrailleSchool thinks of that...
    i always rep
    sending rep over now
    didnt have time before now

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