• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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
0
jettman26
Asked:
jettman26
  • 8
  • 6
1 Solution
 
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
 
S-TwilleyCommented:
still inserts a Tab on Ctrl + I
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
S-TwilleyCommented:
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now