Solved

Passing font characteristics to a function

Posted on 2008-10-20
5
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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