[C#] Block (Un)Indenting in RichTextBox.

L

Leon Friesema

Hiyall!

I have a RichTextBox inside a UserControl which should be able to
indent a block off text (or UnIndent for that matter) like the
SourceEditor of VS2003 or WinWord does.
All controls on the form (except for the RichTextBox) have
TabStop=False and I have a KeyDown event handler which calls the
IndentSelection function. No problem with the indenting (I've watched
when stepping through the code) except.. The e.Handled does not seem
to have any effect. I already created a new KeyUp event handler which
only states: e.Handled = true when a Tab-Key is pressed, but no luck..
Does anybody have any idea where this last TAB comes from? I already
tried the IsInput??? functions, but they have no effect whatsoever...
I'm lost here..

Thanks in advance, greetings,
L.


[CODE SNIPPET]
private void richTextBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyValue == 9) && (e.Shift))
{
if (richTextBox1.SelectionLength != 0)
{
UnIndentSelection();
e.Handled = true;
};
e.Handled = true;
}
else if (e.KeyValue == 9)
{
if (richTextBox1.SelectionLength != 0)
{
IndentSelection();
e.Handled = true;
};
};
}
private void richTextBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyValue == 9)
{
e.Handled = true;
};
}
[/CODE SNIPPET]
 
R

Roland Wolters

Hiyall!

I have a RichTextBox inside a UserControl which should be able to
indent a block off text (or UnIndent for that matter) like the
SourceEditor of VS2003 or WinWord does.
All controls on the form (except for the RichTextBox) have
TabStop=False and I have a KeyDown event handler which calls the
IndentSelection function. No problem with the indenting (I've watched
when stepping through the code) except.. The e.Handled does not seem
to have any effect. I already created a new KeyUp event handler which
only states: e.Handled = true when a Tab-Key is pressed, but no luck..
Does anybody have any idea where this last TAB comes from? I already
tried the IsInput??? functions, but they have no effect whatsoever...
I'm lost here..

Thanks in advance, greetings,
L.


[CODE SNIPPET]
private void richTextBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyValue == 9) && (e.Shift))
{
if (richTextBox1.SelectionLength != 0)
{
UnIndentSelection();
e.Handled = true;
};
e.Handled = true;
}
else if (e.KeyValue == 9)
{
if (richTextBox1.SelectionLength != 0)
{
IndentSelection();
e.Handled = true;
};
};
}
private void richTextBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyValue == 9)
{
e.Handled = true;
};
}
[/CODE SNIPPET]


Ai ai ai wat een crosspost makker! En dan nog hier in het engels ook!
Tssk tssk

Anyway, ik had de oplossing al gemaild, maar voor andere
geinteresseerden hier nog maar eens:

Je zoekt op de verkeerde plaats. Je kunt die tab helemaal niet vangen in
je form, op een keydown event. Je moet daarvoor de method ProcessCmdKey
van het control zelf overriden. Om dat te doen make je gewoon een
inherit class op basis van de richtextbox. (zie snip). Binnen de
ProcessCmdKey ga je vervolgens met je tab key aan de gang. Dit is
overigens ook de plek om andere systeemtoetsen te vangen voordat het OS
met je meedenkt

Enfin vervolgens hoef je in je form helemaal niets meer te doen, geen
toetsen afvangen of wat dan ook Doordat je met een true uit de
ProcessCmdKey springt is het alsof er niets gedrukt is verder.


[Code snip]
using System;
using System.Windows.Forms;

namespace RitchTextBox
{
/// <summary>
/// Summary description for wictRichtTextBox.
/// </summary>
public class wictRichtTextBox: System.Windows.Forms.RichTextBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys
keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;

if ((msg.Msg == WM_KEYDOWN) || (msg.Msg ==
WM_SYSKEYDOWN))
{
switch(keyData)
{
case Keys.Tab:
if (this.SelectionLength != 0)
{

this.SelectionIndent=this.SelectionIndent+10;
}
return true;
break;
case Keys.Shift | Keys.Tab:
if (this.SelectionLength != 0)
{

this.SelectionIndent=this.SelectionIndent-10;
}
return true;
break;
}
}

return base.ProcessCmdKey(ref msg,keyData);
}
}
}

[/Code snip]
 
Top