Fonts, FontFamily & FontStyle / Warum bekomm ich eine exeption?!?

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Fonts, FontFamily & FontStyle / Warum bekomm ich eine exeption?!?

    Hi!
    Ich hab auf meiner Form eine ListView mit Detail-Ansicht und eine RichTextBox mit irgend nem Text.
    In der ListView lasse ich mir alle installierten Schriften auflisten und möchte dann beim klicken auf einen anderen LV-Eintrag die Schriftart bzw. Schrift in der RTB verändern!

    Nur bei einigen Schrifen, z.B. bei Aharoni, bekomm ich eine exeption das der FontStyle "Regular" nicht für diese Schrift zur verfügung steht..
    ..nun hab ich aber eine Abfrage, welche prüft ob die Schriftart mit dem FontStyle vorhanden ist!?!

    Also ich weiß das die Schrift Aharoni als Standart "Bold" ist, aber das sollte mir die Funktion eig. zurück geben!!

    Wo ist der Fehler? ;(

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Drawing.Text
    2. Public Class Form1
    3. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4. For Each _font As Font In getAllFonts()
    5. Dim lvItem As New ListViewItem
    6. lvItem.Text = _font.Name
    7. lvItem.Font = _font
    8. lvFonts.Items.Add(lvItem)
    9. rtb.SelectAll()
    10. Next
    11. End Sub
    12. Private Function getAllFonts() As List(Of Font)
    13. Dim _lOfFonts As New List(Of Font)
    14. Dim fonts As InstalledFontCollection = New InstalledFontCollection()
    15. For Each _font As FontFamily In fonts.Families
    16. _lOfFonts.Add(_checkFont(_font.Name.ToString))
    17. Next
    18. Return _lOfFonts
    19. End Function
    20. Private Function _checkFont(ByVal _font As String) As Font
    21. Dim __font As Font = Nothing
    22. If DoesFontExist(_font, FontStyle.Regular) = True Then
    23. __font = New Font(_font, 14, FontStyle.Regular)
    24. Else
    25. If DoesFontExist(_font, FontStyle.Bold) = True Then
    26. __font = New Font(_font, 14, FontStyle.Bold)
    27. Else
    28. If DoesFontExist(_font, FontStyle.Italic) = True Then
    29. __font = New Font(_font, 14, FontStyle.Italic)
    30. End If
    31. End If
    32. End If
    33. Return __font
    34. End Function
    35. Private Function DoesFontExist(fontFamilyName As String, fontStyle As FontStyle) As Boolean
    36. Dim result As Boolean
    37. Try
    38. Using family As New FontFamily(fontFamilyName)
    39. result = family.IsStyleAvailable(fontStyle)
    40. End Using
    41. Catch generatedExceptionName As ArgumentException
    42. result = False
    43. End Try
    44. Return result
    45. End Function
    46. Private Sub lvFonts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvFonts.SelectedIndexChanged
    47. If lvFonts.SelectedItems.Count > 0 Then
    48. Dim fs As FontStyle = rtb.Font.Style
    49. Dim _nFont As Font = New Font(_checkFont(lvFonts.Items(lvFonts.FocusedItem.Index).SubItems(0).Text).FontFamily.Name.ToString, 14, fs)
    50. rtb.SelectAll()
    51. rtb.SelectionFont = _nFont
    52. End If
    53. End Sub
    54. End Class
    Käy, hab da iwie den FontStyle falsch gesetzt! :whistling:

    Aber brauch ne zusätzliche Function zur FontStyle abfrage..habs nu so gelöst:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Function _getFS(ByVal _font As Font) As Integer
    2. Dim result As Integer = 0
    3. Dim isRegular = _font.Style And FontStyle.Regular ' isBold is 0 if not, 0 if it is
    4. Dim isBold = _font.Style And FontStyle.Bold ' isBold is 0 if not, 1 if it is
    5. Dim isItalic = _font.Style And FontStyle.Italic ' isItalic is 0 if not, 2 if it is
    6. Dim isUnderline = _font.Style And FontStyle.Underline ' isItalic is 0 if not, 4 if it is
    7. Dim isStrikeout = _font.Style And FontStyle.Strikeout ' isItalic is 0 if not, 8 if it is
    8. result = isRegular + isBold + isItalic + isUnderline + isStrikeout
    9. Return result
    10. End Function
    11. Private Sub lvFonts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvFonts.SelectedIndexChanged
    12. If lvFonts.SelectedItems.Count > 0 Then
    13. Dim _fs As FontStyle = rtb.Font.Style
    14. Dim cap As Integer = _getFS(_checkFont(lvFonts.Items(lvFonts.FocusedItem.Index).SubItems(0).Text))
    15. If cap <> 0 Then
    16. If cap = 1 Then
    17. _fs = FontStyle.Bold
    18. End If
    19. If cap = 2 Then
    20. _fs = FontStyle.Italic
    21. End If
    22. End If
    23. Dim _nFont As Font = New Font(_checkStyle(lvFonts.Items(lvFonts.FocusedItem.Index).SubItems(0).Text, _fs).FontFamily.Name.ToString, 14, _fs)
    24. rtb.SelectAll()
    25. rtb.SelectionFont = _nFont
    26. End If
    27. End Sub

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Morrison“ ()