Fontstyle Available?

Graham

Member
Joined
Feb 17, 2007
Messages
13
Programming Experience
5-10
I'm writing a simple text editor, I have a combobox to select a given font, the problem is that if the font doesn't support Regular then the program crashes.
How do I get around this? below is the code
VB.NET:
Private Sub Style()
        If Not rtbText.SelectionFont Is Nothing Then

            Dim currentFont As System.Drawing.Font = rtbText.SelectionFont
            Dim newFontStyle As System.Drawing.FontStyle

            If Bold = True Then newFontStyle = newFontStyle Or FontStyle.Bold
                If Italic = True Then newFontStyle = newFontStyle Or FontStyle.Italic
                If Strike = True Then newFontStyle = newFontStyle Or FontStyle.Strikeout
                If Under = True Then newFontStyle = newFontStyle Or FontStyle.Underline

                ' Get the font size.
                Dim font_size As Single = 10
                Try
                    font_size = Single.Parse(cboSize.SelectedItem)
                Catch ex As Exception
                End Try

            rtbText.SelectionFont = New Font(currentFont.FontFamily, font_size, newFontStyle)

        End If
    End Sub
 
Thanks Vis
Already using IsStyleAvailable to load fonts into ComboBox, just couldn't figure how to code it for when the font without Regularstyle was chosen
I've now done this and it seems to work, what do you think?

VB.NET:
 Private Sub Style()
        If Not rtbText.SelectionFont Is Nothing Then

            Dim currentFont As System.Drawing.Font = rtbText.SelectionFont
            Dim newFontStyle As System.Drawing.FontStyle

            If Bold = True Then newFontStyle = newFontStyle Or FontStyle.Bold
                If Italic = True Then newFontStyle = newFontStyle Or FontStyle.Italic
                If Strike = True Then newFontStyle = newFontStyle Or FontStyle.Strikeout
                If Under = True Then newFontStyle = newFontStyle Or FontStyle.Underline

                ' Get the font size.
                Dim font_size As Single = 10
                Try
                    font_size = Single.Parse(cboSize.SelectedItem)
                Catch ex As Exception
                End Try
            If currentFont.FontFamily.IsStyleAvailable(FontStyle.Regular) Then
                rtbText.SelectionFont = New Font(currentFont.FontFamily, font_size, newFontStyle)
            Else
                If currentFont.FontFamily.IsStyleAvailable(FontStyle.Italic) Then
                    rtbText.SelectionFont = New Font(currentFont.FontFamily, font_size, FontStyle.Italic)
                Else
                    If currentFont.FontFamily.IsStyleAvailable(FontStyle.Bold) Then
                        rtbText.SelectionFont = New Font(currentFont.FontFamily, font_size, FontStyle.Bold)
                    End If
            End If
        End If
        End If
    End Sub
 
Back
Top