Changing a default behavior of RichTextBox

M

MindWanderer

I have created a rich text box for my application and a tool strip
which has bold, italic, underline and other formatting icons on it,
that affect the selected text's formatting. I've also written a key
down event handler for the form the text box is in that handles
keyboard shortcuts.

All my shortcuts work right except Control I. It seems Control I has
built in functionality that tabs the RichTextBox, so when you try to
make something italic now with the shortcut you tab and your selection
is italic. I'm looking for a way to disable this built in shortcut, as
I've enabled regular tabs to be used in the RichTextBox anyway with
AcceptsTab.

Any idea what I need to do to stop the default behavior of Control I?
 
A

AMercer

Any idea what I need to do to stop the default behavior of Control I?

in addition to all you are currently doing, add a KeyPress handler for your
rtb that does this:
If e.KeyChar = vbTab Then e.Handled = True
Your key down or preview key down should handle the key for your purposes,
and this should stop the key from getting to the rtb. If you are not already
doing it, you may need to set e.Handled to true in key down.

Key handling is a mess, imo, owing to years windows evolution and the goal
of backward compatibility.
 
M

MindWanderer

Thanks for the info.

There's been a lot of pitfalls involved with it for sure. At first I
forgot to set the Form property that enables key handling at all and
that was really confusing.
 
M

MindWanderer

Thanks for the info.

There's been a lot of pitfalls involved with it for sure. At first I
forgot to set the Form property that enables key handling at all and
that was really confusing.

Your info helped solve half the problem, but it then disabled regular
tab functionality, which is more needed in this case than italics. But
I was able to find a workaround:

Omitting parts that are irrelevant, this is my Entry class, extending
Form

Basically, this sets TabEnabled to false whenever Control is pressed
down, and to true when its pressed up. Therefore when TabEnabled is
false (and therefore control is currently pressed), tabs are
surpressed, whereas otherwise they are not.

private bool TabEnabled; The constructor sets this to
true.

private void Entry_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
HandleControlCodes(e);
TabEnabled = false;
}
}

private void HandleControlCodes(KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
richTextBox1.SelectAll();
return;
}
else if (e.KeyCode == Keys.B || e.KeyCode == Keys.I ||
e.KeyCode == Keys.U)
ProcessStyleChange(e);
else if (e.KeyCode == Keys.S)
saveToolStripMenuItem_Click(null, null);
}

private void ProcessStyleChange(KeyEventArgs e)
{
bool bold = false;
bool italic = false;
bool underline = false;

if (e.KeyCode == Keys.B)
bold = true;
if (e.KeyCode == Keys.I)
{
italic = true;
}
if (e.KeyCode == Keys.U)
underline = true;

UpdateFontStyle(bold, italic, underline);
}

public void UpdateFontStyle(bool bo, bool it, bool un)
{
bool bold = richTextBox1.SelectionFont.Bold ^ bo;
bool italic = richTextBox1.SelectionFont.Italic ^ it;
bool underline = richTextBox1.SelectionFont.Underline ^
un;

FontStyle builtStyle = FontStyle.Regular;

if (bold)
builtStyle |= FontStyle.Bold;
if (italic)
builtStyle |= FontStyle.Italic;
if (underline)
builtStyle |= FontStyle.Underline;

richTextBox1.SelectionFont = new
Font(richTextBox1.SelectionFont, builtStyle);
}

private void richTextBox1_KeyPress(object sender,
KeyPressEventArgs e)
{
if (TabEnabled)
return;

if (e.KeyChar == ((char)9))
e.Handled = true;
}

private void Entry_KeyUp(object sender, KeyEventArgs e)
{
if(e.Control)
TabEnabled = true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top