Windows Develop Bookmark and Share   
 index > Windows Forms General > Control-I in RichTextBox is seen as a horizontal tab command
 

Control-I in RichTextBox is seen as a horizontal tab command

Hello,

I have code that makes the selected text italics and works great from a toolstrip menu, but when I go to use the key press code to do the same thing I noticed that the control is seeing the combination (Control-I) as the ASCII command for horizontal tab, which is the default for ASCII, but I would like it to not do that since that is not standard MS behaviour, e.g. Word, Outlook, Excel, etc. etc.

I have tried suppressing on key up, key down and key press events, but the process still seems to add the horizontal tab in prior to the code thread getting handed to the control.

If anyone could shed some light on how to suppress or turn off the horizontal tab from a control-I key press, I would greatly appreciated it.

Thanks!
BrianMize  Friday, September 11, 2009 3:51 PM
I helped jimmygyuma with exactly the same issue on may last year. So I hope the same answer will also help you: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/9cae9c90-8b79-47dc-b899-25a45aebd9b1 .

You say that suppressing key up/down didn't work? That's weird: it worked for me when I tested the reply for that thread, and worked for jimmygyuma as well. I'd recommend that you review the code, just for case: is the handler method actually bound to the event? If that doesn't solve the issue, check the thread I linked above and compare it with your own code: which are the significant differences? We might need to know that to figure out why this approach isn't worked for you.

Hope this helps.

Regards,
Herenvardo
  • Edited byherenvardo Friday, September 11, 2009 11:01 PMAdded more troubleshooting info
  • Marked As Answer byBrianMize Saturday, September 12, 2009 6:03 PM
  •  
herenvardo  Friday, September 11, 2009 10:52 PM
This worked well:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == (Keys.Control | Keys.I)) {
FontStyle style = richTextBox1.SelectionFont.Style;
if ((style & FontStyle.Italic) == FontStyle.Italic)
style &= ~FontStyle.Italic;
else style |= FontStyle.Italic;
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
e.SuppressKeyPress = true;
}
}


Hans Passant.
  • Marked As Answer byBrianMize Saturday, September 12, 2009 6:03 PM
  •  
nobugz  Saturday, September 12, 2009 2:31 PM
I helped jimmygyuma with exactly the same issue on may last year. So I hope the same answer will also help you: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/9cae9c90-8b79-47dc-b899-25a45aebd9b1 .

You say that suppressing key up/down didn't work? That's weird: it worked for me when I tested the reply for that thread, and worked for jimmygyuma as well. I'd recommend that you review the code, just for case: is the handler method actually bound to the event? If that doesn't solve the issue, check the thread I linked above and compare it with your own code: which are the significant differences? We might need to know that to figure out why this approach isn't worked for you.

Hope this helps.

Regards,
Herenvardo
  • Edited byherenvardo Friday, September 11, 2009 11:01 PMAdded more troubleshooting info
  • Marked As Answer byBrianMize Saturday, September 12, 2009 6:03 PM
  •  
herenvardo  Friday, September 11, 2009 10:52 PM
This worked well:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == (Keys.Control | Keys.I)) {
FontStyle style = richTextBox1.SelectionFont.Style;
if ((style & FontStyle.Italic) == FontStyle.Italic)
style &= ~FontStyle.Italic;
else style |= FontStyle.Italic;
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, style);
e.SuppressKeyPress = true;
}
}


Hans Passant.
  • Marked As Answer byBrianMize Saturday, September 12, 2009 6:03 PM
  •  
nobugz  Saturday, September 12, 2009 2:31 PM

Thanks guys! Both of your suggestions were on the same track as I was going, but I put a msgbox in the method to let me know that it found it and then did the suppression, but since the message box threw off the thread, it caused some odd behaviour. Taking the box out let the function work.

The suppression worked also after I constructed the test correctly and threw the suppression first.

So my code (especially for you other VB folks out there :) ) is as such:

Private Sub txtMessage_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyDown
If (e.KeyCode = Keys.I And Keys.Control) Then

e.SuppressKeyPress = True

Dim currentFont As System.Drawing.Font = Nothing
Dim currentStyle As System.Drawing.FontStyle = Nothing
Dim newFontStyle As System.Drawing.FontStyle = Nothing

currentFont = txtMessage.SelectionFont
currentStyle = currentFont.Style

If txtMessage.SelectionFont.Italic = True Then
newFontStyle = currentStyle - FontStyle.Italic
Else
newFontStyle = currentStyle + FontStyle.Italic
End If

txtMessage.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, newFontStyle)

End If
End Sub

Thanks for taking the time to respond and you insight! I'm glad I wasn't too far off the mark!
B

BrianMize  Saturday, September 12, 2009 6:02 PM
Your code is not correct. You are comparing a FontStyle to a Boolean. Italic happens to be 2, it will never equal to True. Your code will turn Italic on but will never turn it off. Use Option Explicit to catch nasty mistakes like this.

Hans Passant.
nobugz  Saturday, September 12, 2009 7:00 PM

You can use google to search for other answers

Custom Search

More Threads

• how to activate a MDI child
• capturing the form appearence?
• Change Focus after Move
• Button pressed past text into textbox
• Marquee property of ProgressBar is not working
• A timer .. no I jsut need to pause for X
• ceate executable file !
• Printing Text In A RichTextBox
• Forms and McAfee Viruscan 8.0.i
• Issue when Printing using WebBrowser (Windos.Forms) in Console Application