Knightlite_James
asked on
Saving Screen Font Names In Visual Basic 2008?
Hi,
I am trying to find a way to save screen font names for a few text boxes to the Registry and then have the correct fonts load when the forms loads.
I pick the font, it saves the name to the Registry, and when I run the program again, the correct font name loads for the textbox. See the code below. This is what I have tried to do, but it does not work.
Does anyone know a way I can get this to wrk? Any help would be greatly appreciated.
James
I am trying to find a way to save screen font names for a few text boxes to the Registry and then have the correct fonts load when the forms loads.
I pick the font, it saves the name to the Registry, and when I run the program again, the correct font name loads for the textbox. See the code below. This is what I have tried to do, but it does not work.
Does anyone know a way I can get this to wrk? Any help would be greatly appreciated.
James
'This code saves the Font Name to the Registry
Private Sub ChangeScreenFontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeScreenFontToolStripMenuItem.Click
'Change the screen font and save it to a database.
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
txt1.Font = FontDialog1.Font
txt2.Font = FontDialog1.Font
End If
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Font", FontDialog1.Font)
End Sub
'And this code is to get it back when the form loads, but I get an error --> Value of type 'String' cannot be converted to 'System.Drawing.Font'
Dim F As Font
F = CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Font", String.Empty))
txt1.Font = F
txt2.Font = F
ASKER
Hi Randy,
Thank you very much for taking the time to help me. I really appreciate it.
I ran the code listed below and I get an error on the line:
Dim SavedFont As New Font(FontName, FontSize)
The error says the following:
InvalidCastException was unhandled.
Conversion from string "[Font: Name=Microsoft Sans Serif" to type 'Single' is not valid.
Do you know what may be causing this error and how to fix it?
Sincerely,
James
Thank you very much for taking the time to help me. I really appreciate it.
I ran the code listed below and I get an error on the line:
Dim SavedFont As New Font(FontName, FontSize)
The error says the following:
InvalidCastException was unhandled.
Conversion from string "[Font: Name=Microsoft Sans Serif" to type 'Single' is not valid.
Do you know what may be causing this error and how to fix it?
Sincerely,
James
Dim FontName As String = CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Font", String.Empty))
Dim FontSize As String = CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Font", String.Empty))
Dim SavedFont As New Font(FontName, FontSize)
txtQuestion.Font = SavedFont
txtA.Font = SavedFont
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Randy,
That worked really well. The only thing it does not do is load or save, is if the font is Bold or Italic, but I guess I could do that through the Rich Text Box.
Thanks again for your help. I really appreciate you taking the time to help.
James
That worked really well. The only thing it does not do is load or save, is if the font is Bold or Italic, but I guess I could do that through the Rich Text Box.
Thanks again for your help. I really appreciate you taking the time to help.
James
Hello, James,
Re: "The only thing it does not do is load or save, is if the font is Bold or Italic"
You might want to look at the minor additions in the attached snippet. I think that it will provide what you need.
Cheers,
Randy
Re: "The only thing it does not do is load or save, is if the font is Bold or Italic"
You might want to look at the minor additions in the attached snippet. I think that it will provide what you need.
Cheers,
Randy
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Change the screen font and save it to a database.
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Font = FontDialog1.Font
TextBox2.Font = FontDialog1.Font
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Font", FontDialog1.Font)
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Style", FontDialog1.Font.Style)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim FontDescription As String = CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Font", String.Empty))
Dim FontDescArray As String() = FontDescription.Split("="c, ","c)
Dim FontName As String = FontDescArray(1)
Dim FontSize As Single = CSng(FontDescArray(3))
Dim strStyle As String = CStr(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Knightlite\Signal 102 V5\Registration\FontName", "Style", FontStyle.Regular))
Dim SavedStyle As FontStyle = DirectCast([Enum].Parse(GetType(FontStyle), strStyle, True), FontStyle)
Dim SavedFont As New Font(FontName, FontSize, SavedStyle)
TextBox1.Font = SavedFont
TextBox2.Font = SavedFont
End Sub
What you are getting back from the registry is a string that provides the Font's description. Something like:
"[Font: Name=Times New Roman, Size=9.75, Units=3, GdiCharSet=0, GdiVerticalFont=False]".
You will need to parse out the font name and size. The String's "Split" method can be very helpful for this purpose.
Dim FontName As String = ...
Dim FontSize As Single = ...
Then use those values in a statement like that following.
Dim SavedFont As New Font(FontName, FontSize)
Then you can assign the font to the TextBoxes:
txt1.Font = SavedFont
txt2.Font = SavedFont
Cheers,
Randy