I need the program to convert a font to a string and a string to a font. I can't pass the font directly to the text file, because it needs to be converted to a string. Also, I can't use a string from a text file to set the font, because I need to convert the string to a font to use it. How do I do this? There must be some general built-in function to do this.
Also, some thing I didn't think of earlier:
How do I convert the colours (like 255, 188, 0) to strings?
How do I convet preset colours (like Color.LightGreen) to strings?
How do I convert strings (like "Color.LightGreen" to colours?
I look forward to your reply.
Converting a colour to a string is relatively simple. The question is, do you actually need to save it as RGB values, or would it be sufficient to save the colour number? In other words, why store
255, 188, 0 when you can just store
48383 (decimal) or
BCFF (hex)? They're just different ways of representing the same number, but separate RGB values entail extra work in splitting up the colour into it’s components (and putting them back together).
As for the "preset" colours like LightGreen, I think you will find that they are just a convenient way of referring to a number. You can just save the number - does it matter that it was originally referenced by a name?
Converting a string like "Color.LightGreen" back to an actual colour might involve a lot of IF...ELSE... type processing. But if you just store the number rather than the name, that should be all you need to worry about when reading it back.
Converting a font back and forth may not be too hard. Remember that I'm using VB6, so things may work differently for you. But given a font object, you can decide which properties you're interested in and just place them in your string. I've put together a working sample in VB6 which illustrates one method of converting both ways between a font and a string. You will probably need to adapt it for VB.Net, sorry (best I can do in my lunch hour). Anyway, here are the steps to create the sample...
Create a form called Form1, and place on it a Textbox called Text1. Set the font of the form (or textbox) to something visually quite different to the default (it doesn't matter what, as long as you can see a difference between text on the form and in the textbox). Create these two functions in the code module...
- Option Explicit
-
DefLng A-Z
-
Private MyArray() As String
-
- Public Function FontToString(f As StdFont) As String
-
With f
-
FontToString = .Bold & "|" & _
-
.Italic & "|" & _
-
.Size & "|" & _
-
.Strikethrough & "|" & _
-
.Underline & "|" & _
-
.Weight & "|" & _
-
.Name
-
End With
-
End Function
-
- Public Sub StringToFont(s As String, f As StdFont)
-
Erase MyArray
-
MyArray() = Split(s, "|")
-
With f
-
.Name = MyArray(6)
-
.Bold = (MyArray(0) = "True")
-
.Italic = (MyArray(1) = "True")
-
.Size = Val(MyArray(2))
-
.Strikethrough = (MyArray(3) = "True")
-
.Underline = (MyArray(4) = "True")
-
.Weight = Val(MyArray(5))
-
End With
-
End Sub
In the click event for the form, place this code...
- Dim s As String
-
s = FontToString(Me.Font)
-
StringToFont s, Text1.Font
-
As an example of what this does, here’s the result when I invoked the FontToString function from the Immediate window...
- Print FontToString(Form1.Font)
- True|True|15.75|True|True|700|Palatino Linotype
Oh! I need to correct myself. I thought that names like LightGreen were just simple numeric constants like vbGreen and so on. But I had a look at
the doco on the MSDN site and of course it’s not that simple. I don’t know yet how the Color structure works. But we can work on that, if necessary.