Click here to Skip to main content
14,691,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on creating an application that includes a text editor. I need to give some indication to the user about the current insert mode of the editor box - (will it insert the characters or overwrite them?). The RichTextBox processes the Insert key well - toggling the insert mode on every key press. But, I did not find a way of querying the RichTextBox, to find what is the current insert mode. Tried to google around for it, without much luck.

One way I thought of, is to handle the insert key processing and the insert / overwrite logic, within my code - so that I can control the insert mode and display the notification. But that would mean unnecessary duplication of a functionality that is already built in.

I am sure, if the RichTextBox processes the insert key, there must be some way of checking the current mode. Can someone help me with this?
Updated 3-Oct-11 18:42pm
Tag it! WPF, Forms, what?

1 solution

With WPF, you can checkup System.Windows.Input.Keyboard.IsKeyToggled(Key.Insert), see[^].

Unfortunately, there is no such thing in System.Windows.Forms. One work-around is using Windows API GetKeyState, via P/Invoke, see[^]. For toggled state you need to examine low-order bit of the result, so mask it like this result = GetKeyState(/*...*/) & 1.

Unfortunately, using P/Invoke would break platform compatibility — the code will work only on Windows (a Forms application could work well on many platforms without recompilation under Mono,[^],[^]). By this reason, your approach which does not require P/Invoke is better.

There is one more reason: every instance of System.Windows.Forms.RichTextBox control has its own insert state that is independent of the global insert state as well as the insert state of all other RichTextBox controls. Take a look at this text:[^] (sorry, it's in VB.NET, but you can understand it). See also this discussion:[^].

You are right about unnecessary duplication of a functionality, but this functionality is not expose, which is a shame, I would say. So, you will need to handle some key event anyway, because even when you can poll the keyboard status, you need some event to trigger your check.

Vikas K Solegaonkar 3-Oct-11 23:41pm
Thanks for your help with this... the and codeguru links are good.
You're welcome.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900