Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 ...

 

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

  • *Gurus*
Posted

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:

 

   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

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...