Solved

Passing font characteristics to a function

Posted on 2008-10-20
5
230 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
ID: 22766853
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
ID: 22768655
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
ID: 22772631
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
ID: 22773375
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
ID: 31508114
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

24 Experts available now in Live!

Get 1:1 Help Now