Hi everyone :wave:
.
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 :blush:
Printable View
Hi everyone :wave:
.
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 :blush:
During runtime?
Then you need to save these settings somewhere and load them when the application starts.
Thank you dear Arnoutdv, but how? can you show me?
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)?
Attachment 143465
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
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.
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
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
Thank you dear for this code
:)
He did.
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.
Attachment 143487
Thank you dear dilettante for your nice code! it is very helpful