Sample codes on FontCollection didn't work...

  • Thread starter pedestrian via DotNetMonster.com
  • Start date
P

pedestrian via DotNetMonster.com

I'm trying to get all the font names (types) so I use the following code
from MSDN which entitled : "How to: Enumerate Installed Fonts"
(refer ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.
en/dv_fxmclignrl/html/26d74ef5-0f39-4eeb-8d20-00e66e014abe.htm)

I tried to put them in a WinForm (Form load and button click events)
however it showed 2 errors:

Error 1 Type 'InstalledFontCollection' is not defined.
Error 2 'Graphics' is not a member of 'System.EventArgs'.

I have import the System.Drawing(.Text) namespaces, What's the possible
errors and missing parts?
Thanks in advance.


Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
fontFamily, _
8, _
FontStyle.Regular, _
GraphicsUnit.Point)
Dim rectF As New RectangleF(10, 10, 500, 500)
Dim solidBrush As New SolidBrush(Color.Black)

Dim familyName As String
Dim familyList As String = ""
Dim fontFamilies() As FontFamily

Dim installedFontCollection As New InstalledFontCollection()

' Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families

' The loop below creates a large string that is a comma-separated
' list of all font family names.
Dim count As Integer = fontFamilies.Length
Dim j As Integer

While j < count
familyName = fontFamilies(j).Name
familyList = familyList + familyName
familyList = familyList + ", "
j += 1
End While

' Draw the large string (list of all families) in a rectangle.
e.Graphics.DrawString(familyList, font, solidBrush, rectF)

--
Regards,
Pedestrian, Penang.

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200611/1
 
C

Chris Dunaway

pedestrian said:
I'm trying to get all the font names (types) so I use the following code
from MSDN which entitled : "How to: Enumerate Installed Fonts"
(refer ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.
en/dv_fxmclignrl/html/26d74ef5-0f39-4eeb-8d20-00e66e014abe.htm)

I tried to put them in a WinForm (Form load and button click events)
however it showed 2 errors:

Error 1 Type 'InstalledFontCollection' is not defined.
Error 2 'Graphics' is not a member of 'System.EventArgs'.

I have import the System.Drawing(.Text) namespaces, What's the possible
errors and missing parts?

For error 1, have you added a reference to the System.Drawing.Text
assembly? And have you added the Imports statement at the top of the
code?

Error 2 is pretty much self explanatory. The EventArgs that is passed
into the Form Load and Button click events does not have a Graphics
property. That drawing code needs to be in the Paint event where the
graphics property is passed. Alternatively, you need to create a
Graphics object for the form and use that to draw with. Be sure that
if you create a Graphics object, that you call Dispose on it when you
are through. If you use the Paint event do NOT call Dispose on that
Graphics instance since you did not create it.
 
P

pedestrian via DotNetMonster.com

Thanks for your assistance, Mr Chris.

Chris said:
I'm trying to get all the font names (types) so I use the following code
from MSDN which entitled : "How to: Enumerate Installed Fonts"
[quoted text clipped - 9 lines]
I have import the System.Drawing(.Text) namespaces, What's the possible
errors and missing parts?

For error 1, have you added a reference to the System.Drawing.Text
assembly? And have you added the Imports statement at the top of the
code?

Error 2 is pretty much self explanatory. The EventArgs that is passed
into the Form Load and Button click events does not have a Graphics
property. That drawing code needs to be in the Paint event where the
graphics property is passed. Alternatively, you need to create a
Graphics object for the form and use that to draw with. Be sure that
if you create a Graphics object, that you call Dispose on it when you
are through. If you use the Paint event do NOT call Dispose on that
Graphics instance since you did not create it.

--
Regards,
Pedestrian, Penang.

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200611/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top