Solved

Passing font characteristics to a function

Posted on 2008-10-20
5
218 Views
Last Modified: 2010-04-23
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?
0
Comment
Question by:glenn_grubb
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
That sure looks like a lot of code to do this:

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

0
 
LVL 16

Expert Comment

by:HooKooDooKu
Comment Utility
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 )
0
 

Author Comment

by:glenn_grubb
Comment Utility
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.
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
Comment Utility
I tried to actually run the code and after some debugging I realized the 1st few lines need some extra parenthases.  The existing code...
 Dim Italic As Boolean = (f.Style And FontStyle.Italic = FontStyle.Italic)
... needs to me modified to ...
 Dim Italic As Boolean =( (f.Style And FontStyle.Italic) = FontStyle.Italic)
...Without the extra parentheses all the styles are getting set to TRUE.

Try the given code snippet in a new project with a RichTextBox and a Control  Button on the form.  I find it working just fine.  The only other thing that comes to mind is if you are getting confused between .SelectionFont (Font of the Selected Text) and .Font (Font for the unselected text).
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        RichTextBox1.SelectionFont = ToggleFontStyle(RichTextBox1.SelectionFont, FontStyle.Bold)

    End Sub

    Private 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 fstyle As New FontStyle()

        If (Bold) Then fstyle = fstyle Or FontStyle.Bold

        If (Italic) Then fstyle = fstyle Or FontStyle.Italic

        If (Strikeout) Then fstyle = fstyle Or FontStyle.Strikeout

        If (UnderLined) Then fstyle = fstyle Or FontStyle.Underline
 

        Return New Font(f.FontFamily, f.SizeInPoints, fstyle)

    End Function

End Class

Open in new window

0
 

Author Closing Comment

by:glenn_grubb
Comment Utility
That did the trick! I had found another way, but this one is far more elegant than my baling wire solution. Thanks.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now