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.KeyEv entArgs) Handles activeRTB.KeyDown
Select Case e.KeyCode
Case Keys.ControlKey + Keys.B, Keys.ControlKey + Keys.U, Keys.ControlKey + Keys.I
SetCurrentStyle(ToolBar1.B uttons(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
I added this code to my app.
Sub activeRTB_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEv
Select Case e.KeyCode
Case Keys.ControlKey + Keys.B, Keys.ControlKey + Keys.U, Keys.ControlKey + Keys.I
SetCurrentStyle(ToolBar1.B
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
Steve, what happens if you change this to the KeyDown event with Ctrl+I?
Bob
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 ...
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
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)
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
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now that's what I call a solution :)
Bob
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
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)
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?
Are these the Control buttons?
Const WM_KEYDOWN = &H100 '256
Const WM_KEYUP = &H101 '257
Bob
Const WM_KEYUP = &H101 '257
Bob
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).Pu
SetCurrentStyle(ToolBar1.B
End If
Case Keys.I
If e.Control = True Then
e.Handled = True
ToolBar1.Buttons(bItalic).
SetCurrentStyle(ToolBar1.B
End If
Case Keys.U
If e.Control = True Then
e.Handled = True
ToolBar1.Buttons(bUnderlin
SetCurrentStyle(ToolBar1.B
End If
End Select
End Sub