Click to See Complete Forum and Search --> : How to add shorcut key for changing a word to bold When i press Ctrl+B (Like in Word)


srinivasintouch
10-09-2006, 01:55 AM
Hi to all,
I hve a requirement like this,
In my vb.net 2.0 Im using a richTextbox Control.
In this How can i add a short cut key to change a to Changr a word to bold when i press ctrl+B.
Please send me a solution.
Thanx in Advance.

edburdo
10-09-2006, 02:53 PM
You need to take the selected text, and make it bold. I think there is a SelectedText property of the RTF box.

How you trigger that (using keypress events, or a toolbar, etc) is up to you.

arjonesiii
10-09-2006, 05:34 PM
Here's an example that toggles text from regular to bold (or vice versa) when the user types Ctrl-B.

Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.B Then
If e.Control Then
If Not RichTextBox1.SelectionFont Is Nothing Then
Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
Dim newFontStyle As System.Drawing.FontStyle

If RichTextBox1.SelectionFont.Bold = True Then
newFontStyle = FontStyle.Regular
Else
newFontStyle = FontStyle.Bold
End If

RichTextBox1.SelectionFont = New Font( _
currentFont.FontFamily, _
currentFont.Size, _
newFontStyle _
)
End If
End If
End If
End Sub

This example only works on selected text but you should be able to make it toggle individual words, sentences, or paragraphs with a little work.