CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    23

    Question Create a font using a type converter.

    Hello,

    I', attempting to convert a string description into a font and I'm successful except for updating the style.
    I'm using a type converter, here is the code:

    Code:
    Dim tobj As Type = obj.GetType
    
    Dim pi As PropertyInfo = tobj.GetProperty("Font")
    
    Dim propvalue as string = "Tahoma;10"
    
    Dim valueobject As Object = TypeDescriptor.GetConverter(pi.PropertyType).ConvertFromString(propvalue)
    With this propvalue, it works. The valueobject created is a font. But I can't define the style of the font, because if I assign the propvalue as follows:

    Dim propvalue as string = "Tahoma;10;Bold"

    an exception is throwed:

    Run-time exception thrown : System.ArgumentException - Failed to parse Text("Tahoma;10;Bold") expected text in the format "name, size[units[, style]]".
    Does anyone know what text should be into propvalue?

    Thanks in advance.

    NAlvar

  2. #2
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441
    NAlvar,
    as the error message indicates, you are missing the units (pixels, points, inches, ...).

    Valid values for Units: Px, Pt, Pc, In, Mm, Cm, %

    This easier code works for me:
    Code:
            Dim obj As Font = New Font("Tahoma", 10)
            Dim propvalue As String = "Tahoma,12Pt,Bold"
            Dim valueobject As Font = System.ComponentModel.TypeDescriptor.GetConverter(obj).ConvertFromString(propvalue)
    Hope it helps
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

  3. #3
    Join Date
    Feb 2004
    Posts
    23

    Can be possible that it doesn't work properly?

    Dear DeepButi,

    thanks for your advise, but it doesn't work to me.

    After executing your example code, the font defined into the valueobject is the default font :

    ?valueobject.ToString
    "[Font: Name=Microsoft Sans Serif, Size=8, Units=3, GdiCharSet=1, GdiVerticalFont=False]"
    Have you got more ideas?

    Tot i aix*, gr*cies per l'ajuda.

  4. #4
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441
    NAlvar,
    oopppsss

    Dim propvalue As String = "Tahoma,12Pt"

    works ... and I assumed "...,bold" would also, but it doesn't.

    Unable to find the correct names for styles. Will check it.

    PS. De res home, per això estem
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

  5. #5
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441
    NAlvar,
    Code:
        Dim propvalue As String = "Tahoma,12Pt,style=bold"
        Dim valueobject As Font = System.ComponentModel.TypeDescriptor.GetConverter(Font).ConvertFromString(propvalue)
    (easier: obj is not needed)

    Hope it helps
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured