?
Solved

Passing font characteristics to a function

Posted on 2008-10-20
5
Medium Priority
?
275 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

801 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