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
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?
VB.NET-Quellcode
- Imports System.Drawing.Text
- Public Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- For Each _font As Font In getAllFonts()
- Dim lvItem As New ListViewItem
- lvItem.Text = _font.Name
- lvItem.Font = _font
- lvFonts.Items.Add(lvItem)
- rtb.SelectAll()
- Next
- End Sub
- Private Function getAllFonts() As List(Of Font)
- Dim _lOfFonts As New List(Of Font)
- Dim fonts As InstalledFontCollection = New InstalledFontCollection()
- For Each _font As FontFamily In fonts.Families
- _lOfFonts.Add(_checkFont(_font.Name.ToString))
- Next
- Return _lOfFonts
- End Function
- Private Function _checkFont(ByVal _font As String) As Font
- Dim __font As Font = Nothing
- If DoesFontExist(_font, FontStyle.Regular) = True Then
- __font = New Font(_font, 14, FontStyle.Regular)
- Else
- If DoesFontExist(_font, FontStyle.Bold) = True Then
- __font = New Font(_font, 14, FontStyle.Bold)
- Else
- If DoesFontExist(_font, FontStyle.Italic) = True Then
- __font = New Font(_font, 14, FontStyle.Italic)
- End If
- End If
- End If
- Return __font
- End Function
- Private Function DoesFontExist(fontFamilyName As String, fontStyle As FontStyle) As Boolean
- Dim result As Boolean
- Try
- Using family As New FontFamily(fontFamilyName)
- result = family.IsStyleAvailable(fontStyle)
- End Using
- Catch generatedExceptionName As ArgumentException
- result = False
- End Try
- Return result
- End Function
- Private Sub lvFonts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvFonts.SelectedIndexChanged
- If lvFonts.SelectedItems.Count > 0 Then
- Dim fs As FontStyle = rtb.Font.Style
- Dim _nFont As Font = New Font(_checkFont(lvFonts.Items(lvFonts.FocusedItem.Index).SubItems(0).Text).FontFamily.Name.ToString, 14, fs)
- rtb.SelectAll()
- rtb.SelectionFont = _nFont
- End If
- End Sub
- End Class