Question RTB - Determine current RTB.SelectionFont from cursor position

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I have an RTB and several buttons that change the formatting like this:-

VB.NET:
Private Sub SwitchBold(ByVal sender As Object, ByVal e As EventArgs)
 Dim vBold As ToolStripButton = CType(sender, ToolStripButton)
                With EditorRTB
            If .SelectionFont IsNot Nothing Then
                Dim CurrentFont As Drawing.Font = .SelectionFont
                Dim NewFont As Drawing.FontStyle
                If .SelectionFont.Bold = True Then
                    NewFont = CurrentFont.Style - Drawing.FontStyle.Bold
                    With vBold
                        .Image = My.Resources.Bold_Off
                        .ToolTipText = "Bold formatting set to off"
                    End With
                Else
                    NewFont = CurrentFont.Style + Drawing.FontStyle.Bold
                    With vBold
                        .Image = My.Resources.Bold_On
                        .ToolTipText = "Bold formatting set to on"
                    End With
                End If
                .SelectionFont = New Drawing.Font(CurrentFont.FontFamily, CurrentFont.Size, NewFont)
            End If
        End With
    End Sub

I'm trying to find an event within the RTB that I can use to change the buttons from on to off depending upon the .SelectionFont properties at the cursor point..

Any ideas?

Thanks
 
I've managed to sort it - not sure if it's the best way, but added KeyDown and MouseMove event handlers to the RTB, then

VB.NET:
    Private Sub EditorRTBKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        EditorChangePosition()
    End Sub

    Private Sub EditorRTBMouseMoveEvents(ByVal sender As Object, ByVal e As MouseEventArgs)
        EditorChangePosition()
    End Sub

    Private Sub EditorChangePosition()
        Dim vToolStrip As ToolStrip = RFC(EditorForm, "EditorToolstrip")
        Dim vBold As ToolStripButton = vToolStrip.Items("EditorBold")
        If EditorRTB.SelectionFont IsNot Nothing Then
            Dim CurrentFont As Drawing.Font = EditorRTB.SelectionFont
            If EditorRTB.SelectionFont.Bold = True Then
                With vBold
                    .Image = My.Resources.Bold_On
                    .ToolTipText = "Bold formatting set to on"
                End With
            Else
                With vBold
                    .Image = My.Resources.Bold_Off
                    .ToolTipText = "Bold formatting set to off"
                End With
            End If

        End If
    End Sub
 
In that case I would use KeyUp and MouseUp, that is when a mouse selection is final and when key input is done.
Instead SelectionChanged event can be used, it works for any change of selection.
 

Similar threads

Back
Top