dcsimg
Results 1 to 13 of 13

Thread: Saving Font style on close!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    101

    Saving Font style on close!

    Hi everyone
    .
    I changed label's font style, size and position during runtime, but when I close the application and reOpen every thing will return to default style.
    .
    Please help me

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,424

    Re: Saving Font style on close!

    During runtime?
    Then you need to save these settings somewhere and load them when the application starts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    101

    Re: Saving Font style on close!

    Thank you dear Arnoutdv, but how? can you show me?

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,424

    Re: Saving Font style on close!

    Please show some code how you change the font properties.
    Is it a global change or do you have font settings per control?

    Do you know how to do basic File IO (open, read, write of files)?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    101

    Re: Saving Font style on close!

    Name:  my test project.jpg
Views: 80
Size:  22.2 KB

    look at this picture above ^^^

    I changed label7 to bold and its size to 36 but when I close this form and reload it label7 and other labels will return to the size that I determined when I create the form before runtime. Now I want to save this settings in a file, but I don't know how to create and code for it.
    please show me a proper way (INI, TXT, Or IO ) file or something else!
    please help! I'm waiting for your helps

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,855

    Re: Saving Font style on close!

    You could do it in an INI file, a txt file a database, the registry, an xml file or something else of you prefer.

    You were asked to show some code as to how you change it but all you showed was a picture.

    Show the related code and you may get a more targeted response.
    It would also help if you were to tell us if you know how to create, read and write to/from a file, registry or database.

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,424

    Re: Saving Font style on close!

    For all controls you want to let the user modify the font settings you should save the results in a file:

    Also note the sample does not use a valid folder & pathname
    There is no decent error handling, all errors are just ignored

    Untested code ahead!
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      ReadFontSettings
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      SaveFontSettings
    End Sub
    
    
    
    Private Sub SaveFontSettings()
      Dim fID As Integer
      Dim myControl As Control
      
      fID = FreeFile
      
      ' Use a valid folder & pathname here!
      Open "fontsettings.txt" For Output As fID
      
      ' Replace this brute method with more decent error handling code
      On Error Resume Next
      
      For Each myControl In Me.Controls
        With myControl
          Print #fID, .Name & ":fontname=" & .FontName
          Print #fID, .Name & ":fontsize=" & CStr(.FontSize)
          Print #fID, .Name & ":fontbold=" & CStr(CInt(.FontBold))
          Print #fID, .Name & ":fontitalic=" & CStr(CInt(.FontItalic))
        End With
      Next
      
      Close fID
    End Sub
    
    Private Sub ReadFontSettings()
      Dim myControl As Control
      Dim cFontSettings As Collection
      Dim sLine As String, aLine() As String
      Dim sKey As String, sValue As String
      
      Set cFontSettings = New Collection
      
      fID = FreeFile
      
      ' Use a valid folder & pathname here!
      Open "fontsettings.txt" For Input As fID
        Do Until EOF(fID)
          Line Input #fID, sLine
          If Len(sLine) > 0 Then
            aLine = Split(sLine, "=")
            sKey = aLine(0)
            sValue = aLine(1)
            cFontSettings.Add sValue, sKey
          End If
        Loop
      Close fID
      
      ' Replace this brute method with more decent error handling code
      On Error Resume Next
      
      For Each myControl In Me.Controls
        With myControl
          sKey = .Name & ":fontname"
          .FontName = cFontSettings(sKey)
          
          sKey = .Name & ":fontsize"
          .FontSize = CDbl(cFontSettings(sKey))
          
          sKey = .Name & ":fontbold"
          .FontBold = CBool(cFontSettings(sKey))
          
          sKey = .Name & ":fontitalic"
          .FontItalic = CBool(cFontSettings(sKey))
          
        End With
      Next
      
    End Sub

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,460

    Re: Saving Font style on close!

    Mahmood,

    It also depends on how pervasive you want the change to be. Do you want it to be per user? If that's the case, use the registry (SaveSetting and GetSetting). If you want it to be per machine, then use the C:\ProgramData area and stuff it into a file. If you want it to be per particular copy of your executable, then possibly stuff it into an INI file wherever your executable is residing, or you could possibly write some code to patch up the resource area within the executable.

    The original (development) font settings are always going to be what the label has at startup. But you can change it to anything you like in the Form_Load event, and that'll happen before the user ever sees the label. That's the way it's done.

    Regards,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    101

    Re: Saving Font style on close!

    Thank you dear for this code

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    101

    Re: Saving Font style on close!

    Quote Originally Posted by Elroy View Post
    Mahmood,

    It also depends on how pervasive you want the change to be. Do you want it to be per user? If that's the case, use the registry (SaveSetting and GetSetting). If you want it to be per machine, then use the C:\ProgramData area and stuff it into a file. If you want it to be per particular copy of your executable, then possibly stuff it into an INI file wherever your executable is residing, or you could possibly write some code to patch up the resource area within the executable.

    The original (development) font settings are always going to be what the label has at startup. But you can change it to anything you like in the Form_Load event, and that'll happen before the user ever sees the label. That's the way it's done.

    Regards,
    Elroy
    Can you clarify it !

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,936

    Re: Saving Font style on close!

    He did.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,072

    Re: Saving Font style on close!

    Avoid writing crud to the registry at all. It is bad form, deprecated, and over time it fragments and clutters the registry, slowing the system down.

    Just don't do it.

    Instead you can store per-user settings in a file or files within a unique directory your application creates under the [LocalAppData] special folder. That's why it exists!

    Here's a working demo. You can click on a Label to select it. This shows a selection rectangle and enables the "Customize font" menu. Click that and a Font dialog is raised where you can change the font and color.

    Exit and the customizable Label controls' fonts and colors are persisted to [LocalAppData] as a PropertyBag.

    Run the program again and these settings are restored.

    Name:  sshot.png
Views: 62
Size:  2.7 KB
    Attached Files Attached Files

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    101

    Re: Saving Font style on close!

    Thank you dear dilettante for your nice code! it is very helpful

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width