Pages

Saturday, October 25, 2008

how to trap "delete" key event in C#

"delete" key is a special, it has different function from other key. That's why we can't use ordinary key down / key press event. For example, I have richTextBox called "rtbInput", pictureBox called "picLine" and I want to update line number whenever user delete some text. This is my code to do that :

private void rtbInput_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
            if(e.KeyCode == Keys.Delete )                 
                LineNumber.DrawLineNumber(picLine.CreateGraphics(), rtbInput, picLine); 
    //this is my function to draw line number
}
note that function named "rtbInput_PreviewKeyDown" means I attached this handler to previewKeyDown event of the richTextBox.

No comments: