Click here to Skip to main content
15,896,606 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?
Posted
Updated 3-Oct-11 17:42pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Oct-11 22:40pm    
Tag it! WPF, Forms, what?
--SA

1 solution

With WPF, you can checkup System.Windows.Input.Keyboard.IsKeyToggled(Key.Insert), see http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.iskeytoggled.aspx[^].

Unfortunately, there is no such thing in System.Windows.Forms. One work-around is using Windows API GetKeyState, via P/Invoke, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx[^]. 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, http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/[^]). 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: http://vb.mvps.org/hardcore/html/controllingrichtextboxinsertstate.htm[^] (sorry, it's in VB.NET, but you can understand it). See also this discussion: http://www.codeguru.com/forum/showthread.php?t=276369[^].

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.

—SA
 
Share this answer
 
Comments
[no name] 3-Oct-11 23:41pm    
Thanks for your help with this... the vb.mvps.org and codeguru links are good.
Sergey Alexandrovich Kryukov 4-Oct-11 0:14am    
You're welcome.
Good luck, call again.
--SA

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