Link to home
Start Free TrialLog in
Avatar of glenn_grubb
glenn_grubbFlag for United States of America

asked on

Passing font characteristics to a function

I found this code posted by mohzedan that seems to be what I am looking for, but I am having a problem figuring out out to pass the parameters:
Public Function ToggleFontStyle(ByVal f As Font, ByVal Style As FontStyle) As Font
        Dim bold As Boolean = (f.Style And FontStyle.Bold = FontStyle.Bold)
        Dim Italic As Boolean = (f.Style And FontStyle.Italic = FontStyle.Italic)
        Dim UnderLined As Boolean = (f.Style And FontStyle.Underline = FontStyle.Underline)
        Dim Strikeout As Boolean = (f.Style And FontStyle.Strikeout = FontStyle.Strikeout)

        Select Case Style
            Case FontStyle.Bold
                bold = Not bold
            Case FontStyle.Italic
                Italic = Not Italic
            Case FontStyle.Strikeout
                Strikeout = Not Strikeout
            Case FontStyle.Underline
                UnderLined = Not UnderLined
        End Select

        Dim NewFont As Font
        NewFont = New Font(f.FontFamily, f.SizeInPoints, CType(IIf(bold, FontStyle.Bold, FontStyle.Regular), FontStyle) Or _
                                                       CType(IIf(Italic, FontStyle.Italic, FontStyle.Regular), FontStyle) Or _
                                                       CType(IIf(Strikeout, FontStyle.Strikeout, FontStyle.Regular), FontStyle) Or _
                                                       CType(IIf(UnderLined, FontStyle.Underline, FontStyle.Regular), FontStyle))
        Return NewFont
    End Function

just pass it the richtextbox font and the style you want to toggle.
and it will return the new font that you should set to the richtextnox


I am passing the particulars like this:

        Dim MyFont As Font
        Dim fstyle As New FontStyle()
        fstyle = rtbJJEditor.SelectionFont.Style
   
        MyFont = New Font(rtbJJEditor.SelectionFont, fstyle)

        MyFont = ToggleFontStyle(MyFont, 1)  'indicating I want bold
        rtbJJEditor.SelectionFont = MyFont

The code changes the font to Microsoft Sans Serif 8.25 strikethrough no matter what is sent to it.
Any suggestions?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

That sure looks like a lot of code to do this:

Dim MyFont As New Font("Tahoma", 10, FontStyle.Bold Or FontStyle.Italic)

Avatar of HooKooDooKu
HooKooDooKu

I think your problem is in the creating of a new font and not getting all the existing font properties from the current control.

Try something like this:

Dim MyFont as Font
Dim fstyle as NewFontStyle()
fstyle = FontStyle.Bold
MyFont = rtbJJEditor.SelectionFont
rtbJJEditor.SelectionFont = ToggleFontStyle( MyFont, fstyle )
Avatar of glenn_grubb

ASKER

This shows the proper font from the passing argument:
rtbJJEditor.SelectionFont = ToggleFontStyle( MyFont, fstyle )
but when it arrives at the function, the font has been change to MS Sans serif 8.25.
ASKER CERTIFIED SOLUTION
Avatar of HooKooDooKu
HooKooDooKu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick! I had found another way, but this one is far more elegant than my baling wire solution. Thanks.