c# - Undo buffer getting cleared after handling OnKeyDown in TextBox -



c# - Undo buffer getting cleared after handling OnKeyDown in TextBox -

i subclassing textbox:

class editor : textbox

i have overridden onkeydown, because want tabs replaced 4 spaces:

protected override void onkeydown(keyeventargs e) { if (e.keycode == keys.tab) { selectedtext = " "; e.suppresskeypress = true; } }

this works, unfortunately clears undo buffer. end result when user presses tab, ctrl+z doesn't work , 'undo' on right-click menu becomes disabled. problem appears "e.suppresskeypress = true;" part.

does have thought of how around this?

for more info, creating simple text editor, , i'm handling not tab key (as above), come in key. have problem tab , enter. aware problem doesn't exist richtextbox, various reasons want utilize textbox instead.

any help much appreciated, show-stopping problem in project.

thanks, tom

this isn't result of overriding onkeydown, it's you're setting selectedtext (any text modification have same effect). can see commenting out code sets selectedtext while leaving else. won't tab or 4 characters, undo buffer preserved.

according this blog post, should able utilize paste(string) function rather setting selectedtext property , preserve undo buffer:

protected override void onkeydown(keyeventargs e) { if (e.keycode == keys.tab) { paste(" "); e.suppresskeypress = true; } }

c# winforms textbox

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -