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
jettman26Asked:
Who is Participating?
 
S-TwilleyConnect With a Mentor Commented:
ok, in the RichTextBoxPrintCtrl Class add the following

====

        Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean
            If msg.Msg = 256 Or msg.Msg = 257 Then  'keydown or keyup
                If msg.WParam.ToInt32 = 73 Then  ' I key
                    If (MyBase.ModifierKeys And Keys.Control) = Keys.Control Then
                        'msg = Nothing
                        If msg.Msg = 257 Then RaiseEvent ItalicEvent() 'Keyup
                        Return True
                    End If
                End If
            End If
        End Function

        Public Event ItalicEvent()

=====

and rebuild it.

======

back in the main program.. edit and add this:

    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.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
        e.Handled = True
    End Sub

    Private Sub activeItalicEvent() Handles activeRTB.ItalicEvent
        ToolBar1.Buttons(bItalic).Pushed = Not ToolBar1.Buttons(bItalic).Pushed
        SetCurrentStyle(ToolBar1.Buttons(bBold).Pushed, ToolBar1.Buttons(bUnderline).Pushed, ToolBar1.Buttons(bItalic).Pushed)
    End Sub
0
 
S-TwilleyCommented:
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
0
 
Bob LearnedCommented:
Steve, what happens if you change this to the KeyDown event with Ctrl+I?

Bob
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
S-TwilleyCommented:
still inserts a Tab on Ctrl + I
0
 
S-TwilleyCommented:
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.
0
 
S-TwilleyCommented:
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 ...
0
 
Bob LearnedCommented:
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
0
 
S-TwilleyCommented:
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)
0
 
Bob LearnedCommented:
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
0
 
Bob LearnedCommented:
Now that's what I call a solution :)

Bob
0
 
S-TwilleyCommented:
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
0
 
Bob LearnedCommented:
Right ;)  Go big, or go home :))

Bob
0
 
S-TwilleyCommented:
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)
0
 
jettman26Author Commented:
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?
0
 
Bob LearnedCommented:
Const WM_KEYDOWN = &H100  '256
Const WM_KEYUP = &H101 '257

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.