rtbPad.SelBold = Not rtbPad.SelBold (VB)

jonpyne

Newcomer
Joined
May 26, 2003
Messages
1
Location
Moscow, Russia
rtbPad.SelBold = Not rtbPad.SelBold

Okay, one last try before I give up ...

Hi, all !

Would appreciate a kindly shoulder, if anyone has a moment.

A hypothetical text editor, using a RichTextBox control has a menu/toolbar [whatever] with a _bold_ item that can be toggled on/off, just like in MS Word/WordPad.

In VB6, you would have stuck the code in my subject behind it, but this does not work any more, as MS has made the SelectionFont.Bold property read-only and introduced Font objects.

Dunno if this helps, but I was working along the following lines ...

Code:
Dim x As Font = rtbPad.SelectionFont
Dim y As FontStyle = x.Italic Or x.Strikeout Or x.Underline
If x.Bold Then
    rtbPad.SelectionFont = New Font(x, y)
Else
    rtbPad.SelectionFont = New Font(x, y Or FontStyle.Bold)
End If

Anyone feel like giving me a hand ? This must be a pretty common issue, I just haven't got my head round VB .NET yet.

Just in case, I do not want to use FontStyle.Regular, as this would remove other formatting. Nor do I wish to use a FontDialog. I am just looking to get my toggle back ...

thanks

jON
 
Here's an example sub that toggles a font style on and off for a selected piece of text in a richtextbox control, maintaining any other styles the selection has:

Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ToggleStyle(rtb, FontStyle.Bold)
    End Sub

    Private Sub ToggleStyle(ByVal rtb As RichTextBox, ByVal style As FontStyle)
        If (rtb.SelectionFont.Style And style) = style Then
            'Turn off
            rtb.SelectionFont = New Font(rtb.SelectionFont, rtb.SelectionFont.Style Xor style)
        Else
            'Turn on
            rtb.SelectionFont = New Font(rtb.SelectionFont, rtb.SelectionFont.Style Or style)
        End If
    End Sub
 
Back
Top