This is no easy task... First, use a RichTextBox, for flexibility. I would suggest splitting the textbox into it's lines (just use the TextBox1.Lines collection), check the current line (RTB.GetCharFromPos, then RTB.GetLineFromCharIndex), and only scan that line on KeyPress. That'll make it faster.
You'll also need to detect if you pasted code into textbox (I guess by comparing the number of newline characters before and after the KeyPress event goes through) and if code *was* pasted, you need to scan the whole thing, rather than just the current line.
If you have any further, more specific problems, you should post here, but really, there's no right/wrong way to do it. Just optimize it as best you can.