Getting Fontsy
Peter ... a longtime VB 6 programmer who is now working his way into VB.NET ... wrote and asked:
-----------------
I'm pounding my head against VB.NET trying to get some VB6 programs to work.
My program contains the line...
openFileDialog1.InitialDirectory = "%fonts%"
In VB6 the "%fonts%" ended up being understood as the folder in which the system stored its fonts, without needing a hard-wired folder name.
How do I do this in VB.NET 2008?
-----------------
First, a little discussion of "%fonts%". This is called a DirID (Directory ID) in Windows programming. You can use the SetupSetDirectoryId Windows API to create user DirIDs.
DirID's are mainly used by .INF files. These are "information" files used when Windows installs device drivers and other programs. They are plain text files that you can view in Notepad. DirID's really map directly to numbers. System DirID's range from -1 through 32767. Peter is correct in that the fonts directory is assigned to the number 20.
But that doesn't mean that you can use it in programming. For example, if you open a Command Prompt window and type:
echo %windir%
The system will respond with:
C:\Windows
But my Vista Business SP1 system doesn't recognize the %fonts% DirID. The same test gives this result.
echo %fonts%
%fonts%
So ... even if it did work inside a VB program (VB6 or VB.NET), it's not a reliable way to do things. And, ummmmm ...., I sorta doubt that it can be made to work.
VB.NET has a better way to go. Actually, it has several better ways. Let's see what they are.
First, for convenient access, some commonly used directories can be accessed using the My.Computer object.
But, unfortunately, the fonts directory isn't one of them.
For fonts, you can use the the System.Drawing namespace to simply access the names of the fonts:
Dim ff As FontFamily For Each ff In _ System.Drawing.FontFamily.Families ListBox1.Items.Add(ff.Name) Next
It gets even easier than that, however. VB.NET also provides a custom dialog just for fonts: FontDialog. Just add one to your form and avoid any custom coding. To get started, this simple code assigns a user selected font to a TextBox:
If FontDialog1.ShowDialog() = DialogResult.OK Then TextBox1.Font = FontDialog1.Font End If


Comments
No comments yet. Leave a Comment