Results 1 to 7 of 7

Thread: Richtextbox selected text fontstyle

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Richtextbox selected text fontstyle

    In my Rtf Editor I can toggle bold, Italic, and underline. I works fine. Here is the code.

    Code:
      With Me.rtbNotes
                    If btnBold.Checked Then
                        .SelectionFont = New Font(.SelectionFont, _
                                                  .SelectionFont.Style Or FontStyle.Bold)
                    Else
                        .SelectionFont = New Font(.SelectionFont, _
                                                  .SelectionFont.Style Xor FontStyle.Bold)
                    End If
                End With
    Only problem if I uncheck the btnBold, btnItalic elsewhere in my RichTextBox and return to a line that contains bold,Italic font. It does not check the btnBold or BtnItalic. Could someone point me in a direction.

  2. #2
    Addicted Member
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    142

    Re: Richtextbox selected text fontstyle

    Add a handler to the Application.Idle event and check if the richtextbox SelectionFont is bold whenever the application is idle... like this:

    vb.net Code:
    1. Private Sub Idle(ByVal sender As Object, ByVal e As EventArgs)
    2.         Me.btnBold.Checked = rtbNotes.SelectionFont.Bold
    3.     End Sub
    4.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    5.         AddHandler Application.Idle, AddressOf Idle
    6.     End Sub
    7.  
    8.     Private Sub btnBold_Click(sender As Object, e As System.EventArgs) Handles btnBold.Click
    9.         With Me.rtbNotes
    10.             If btnBold.Checked Then
    11.                 .SelectionFont = New Font(.SelectionFont, _
    12.                                           .SelectionFont.Style Or FontStyle.Bold)
    13.             Else
    14.                 .SelectionFont = New Font(.SelectionFont, _
    15.                                           .SelectionFont.Style Xor FontStyle.Bold)
    16.             End If
    17.         End With
    18.     End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Re: Richtextbox selected text fontstyle

    It worked great! How does this work?
    Last edited by darknoobie; Dec 14th, 2012 at 01:36 AM.

  4. #4
    Addicted Member
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    142

    Re: Richtextbox selected text fontstyle

    Quote Originally Posted by darknoobie View Post
    Lets say I have the following sentence.

    My Dog is Brown. If I move my cursor to "Brown" then bold and Italic check buttons will become unchecked. If I put my cursor somewhere in Dog, both the bod and Italic check buttons should become checked. Is this possible?
    Yes it is possible just be sure you put the code to change the font in the Click event of the button (i assume you are using a checkbox with a button appearance) and not the checkchanged event.

    vb.net Code:
    1. Private Sub Idle(ByVal sender As Object, ByVal e As EventArgs)
    2.         Me.btnBold.Checked = rtbNotes.SelectionFont.Bold
    3.         Me.btnItalic.Checked = rtbNotes.SelectionFont.Italic
    4.     End Sub
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.         AddHandler Application.Idle, AddressOf Idle
    8.  
    9.     End Sub
    10.  
    11.     Private Sub btnBold_Click(sender As Object, e As System.EventArgs) Handles btnBold.Click
    12.         With Me.rtbNotes
    13.             If btnBold.Checked Then
    14.                 .SelectionFont = New Font(.SelectionFont, _
    15.                                           .SelectionFont.Style Or FontStyle.Bold)
    16.             Else
    17.                 .SelectionFont = New Font(.SelectionFont, _
    18.                                           .SelectionFont.Style Xor FontStyle.Bold)
    19.             End If
    20.         End With
    21.     End Sub
    22.  
    23.     Private Sub btnItalic_Click(sender As System.Object, e As System.EventArgs) Handles btnItalic.Click
    24.         With Me.rtbNotes
    25.             If btnItalic.Checked Then
    26.                 .SelectionFont = New Font(.SelectionFont, _
    27.                                           .SelectionFont.Style Or FontStyle.Italic)
    28.             Else
    29.                 .SelectionFont = New Font(.SelectionFont, _
    30.                                           .SelectionFont.Style Xor FontStyle.Italic)
    31.             End If
    32.         End With
    33.     End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Re: Richtextbox selected text fontstyle

    Sorry bout the edit. It works great until I try to close the form it causes an exception. "Cannot access a disposed object. Object name: 'a'." Also I have multiple RichTextBox that share same button.

    For example:

    Code:
      
    If rtbNotes.focused then
    With Me.rtbNotes
                If .SelectionFont.Italic Then
                    .SelectionFont = New Font(.SelectionFont, _
                                              .SelectionFont.Style Xor FontStyle.Italic)
                Else
                    .SelectionFont = New Font(.SelectionFont, _
                                              .SelectionFont.Style Or FontStyle.Italic)
                End If
            End With
    elseif rtbNotes2.focused then
    With Me.rtbNotes2
                If .SelectionFont.Italic Then
                    .SelectionFont = New Font(.SelectionFont, _
                                              .SelectionFont.Style Xor FontStyle.Italic)
                Else
                    .SelectionFont = New Font(.SelectionFont, _
                                              .SelectionFont.Style Or FontStyle.Italic)
                End If
            End With
    end If
    Would I just added:

    Private Sub Idle(ByVal sender As Object, ByVal e As EventArgs)
    Me.btnBold.Checked = rtbNotes.SelectionFont.Bold
    Me.btnItalic.Checked = rtbNotes.SelectionFont.Italic
    Me.btnBold.Checked = rtbNotes2.SelectionFont.Bold
    Me.btnItalic.Checked = rtbNotes2.SelectionFont.Italic
    End Sub

  6. #6
    Addicted Member
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    142

    Re: Richtextbox selected text fontstyle

    I guess you should remove the Application.Idle handler on closing the form. That might be what is causing the error.

    Code:
        Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            RemoveHandler Application.Idle, AddressOf Idle
        End Sub
    As for multiple RichTextBoxes sharing the same buttons, that makes my head hurt lol. Maybe try this:

    Code:
        Private Sub Idle(ByVal sender As Object, ByVal e As EventArgs)
            If Me.rtbNotes.Focused Then
                Me.btnBold.Checked = rtbNotes.SelectionFont.Bold
                Me.btnItalic.Checked = rtbNotes.SelectionFont.Italic
            ElseIf Me.rtbNotes2.Focused Then
                Me.btnBold.Checked = rtbNotes2.SelectionFont.Bold
                Me.btnItalic.Checked = rtbNotes2.SelectionFont.Italic
            End If
        End Sub

  7. #7
    Addicted Member
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    142

    Re: Richtextbox selected text fontstyle

    Oh and you will have to switch the logic for the button click events, for example:

    Code:
            Dim rtb As RichTextBox = If(Me.rtbNotes.Focused, rtbNotes, rtbNotes2)
            With rtb
                If btnBold.Checked Then
                    .SelectionFont = New Font(.SelectionFont, _
                                              .SelectionFont.Style Or FontStyle.Bold)
                Else
                    .SelectionFont = New Font(.SelectionFont, _
                                              .SelectionFont.Style Xor FontStyle.Bold)
                End If
            End With

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width