Link to home
Start Free TrialLog in
Avatar of jettman26
jettman26

asked on

How to get keyboard shorcuts to work in RichTextBox?

I have an App with a RichTextBox.  I want to be able to click ctrl-B on my keyboard and make the selected text bold.  
I added this code to my app.

Sub activeRTB_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles activeRTB.KeyDown
        Select Case e.KeyCode

            Case Keys.ControlKey + Keys.B, Keys.ControlKey + Keys.U, Keys.ControlKey + Keys.I
                SetCurrentStyle(ToolBar1.Buttons(3).Pushed, ToolBar1.Buttons(4).Pushed, ToolBar1.Buttons(5).Pushed)
        End Select

    End Sub

HERE IS ALSO SOME RELEVANT CODE FOR THIS.


Private Sub SetCurrentStyle(ByVal NewBold As Boolean, ByVal NewUnderline As Boolean, ByVal NewItalic As Boolean)
        If boolUpdatingStyle Then Exit Sub
        Dim newStyle As New FontStyle
        If NewBold Then newStyle = newStyle + FontStyle.Bold
        If NewUnderline Then newStyle = newStyle + FontStyle.Underline
        If NewItalic Then newStyle = newStyle + FontStyle.Italic

        If Not NewBold And Not NewUnderline And Not NewItalic Then newStyle = FontStyle.Regular

        If activeRTB Is Nothing Then
            'do nothing
        Else
            Dim newFont As New Font(activeRTB.Font, newStyle)
            activeRTB.SelectionFont = newFont
        End If
    End Sub
Avatar of S-Twilley
S-Twilley

Ok this works... but there seems to be a weird event with Ctrl + I... it wants to insert a tab as well... im not sure why... but it still sets to italic, and the others work too... so before you accept the question, need to wait for an expert who can tell us how to cancel this Ctrl + I => Tab feature of controls


    Private Sub RTBKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles activeRTB.KeyUp
        Select Case e.KeyCode
            Case Keys.B
                If e.Control = True Then
                    e.Handled = True
                    ToolBar1.Buttons(bBold).Pushed = Not ToolBar1.Buttons(bBold).Pushed
                    SetCurrentStyle(ToolBar1.Buttons(bBold).Pushed, ToolBar1.Buttons(bUnderline).Pushed, ToolBar1.Buttons(bItalic).Pushed)

                End If

            Case Keys.I
                If e.Control = True Then
                    e.Handled = True
                    ToolBar1.Buttons(bItalic).Pushed = Not ToolBar1.Buttons(bItalic).Pushed
                    SetCurrentStyle(ToolBar1.Buttons(bBold).Pushed, ToolBar1.Buttons(bUnderline).Pushed, ToolBar1.Buttons(bItalic).Pushed)
                End If

            Case Keys.U
                If e.Control = True Then
                    e.Handled = True
                    ToolBar1.Buttons(bUnderline).Pushed = Not ToolBar1.Buttons(bUnderline).Pushed
                    SetCurrentStyle(ToolBar1.Buttons(bBold).Pushed, ToolBar1.Buttons(bUnderline).Pushed, ToolBar1.Buttons(bItalic).Pushed)
                End If

        End Select
    End Sub
Avatar of Bob Learned
Steve, what happens if you change this to the KeyDown event with Ctrl+I?

Bob
still inserts a Tab on Ctrl + I
Not really a problem with my code as such... it's coded into the control itself to insert a tab on ctrl - I ...  if i remove my code, Ctrl I still inserts a tab, and even by passes the "AcceptsTab" property...  and as you can see, I've put e.Handled = True, but even that doesn't prevent anything.
well, its understandable about it bypassing it.... Ctrl + I  is the shortcut for Indent...

http://www.visualbasicforum.com/showthread.php?t=50363

but still can't get this to work for VB.NET ...
I find that when a control does unexpected behavior that I want to override, I just create a new UserControl, and inherit from that control, and figure out the magic to override.

Bob
you mean something like this in the UserControl KeyDown event:

If Anything Else OTher than Ctrl+I
    MyBase.KeyDown(sender, e)    
Else
    e.Handled = True
    Call code to set Italic
End If

??

========

obviously pseudo code... might tidy things up if a user control was created... even though the control they're using is a control which is based around the RichTextBox (i.e. it's already an extended version)
Maybe something like that.  I haven't tried it, but it sounds good to me.  It would take doing to know for sure, though.

Bob
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now that's what I call a solution :)

Bob
lol... thanx....  forced me to learn something new... i suppose you could code in a BoldEvent and an UnderlineEvent as well with little effort... rather than having half and half
Right ;)  Go big, or go home :))

Bob
then again, a whole lot of the handling could be coded into the class now thati think about it.. not really any need to raise an event to be handled externally... could just handle it within the custom RTB class (set the selectionfont to a bold version)
Avatar of jettman26

ASKER

Thanks.  I haven't tried it yet but I am sure it works.  What is the 256 and 257 integers in the Class?  If msg.Msg = 256 Or msg.Msg = 257.
Are these the Control buttons?
Const WM_KEYDOWN = &H100  '256
Const WM_KEYUP = &H101 '257

Bob