Solved

Passing font characteristics to a function

Posted on 2008-10-20
5
245 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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 …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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