Which event captures every edit to a RichTextBox? - c#

I need to update the formatting of a rich text box after any change is made to the text (for example when the user presses a key and inserts a character). How can I do this?
KeyDown occurs too early and would not account for holding a key down anyway, KeyUp occurs too late, and neither would account for a cut or paste with the mouse.
It would be even better to receive to get this event before the change even appears on screen.

Use the TextChanged event to know whenever the text has changed

Related

How to keep keyboard focus on textbox?

Throughout the app I need to use ENTER to advance to next control (same as Tab), and I do that by using EventManager.RegisterClassHandler, KeyDownEvent for textboxes in Application_Startup. But for one particular textbox (which is the input for a barcode scanner), I want to keep the focus to allow multiple scannings. So it has to receive a number, process it, then clear the box and keep the focus for the next read. Barcode reader is automatically sending an ENTER at the end of a read. Textbox must still be able to lose focus by user's choice (like mouse clicking in another box)
Explicitly handle the PreviewKeyDown on that box and set Handled to true on the event arguments. It executes before any KeyDown handlers.

Detect when user is selecting text in RichTextBox

I have a RichTextBox in my application to show the user an activity log, this RichTextBox is updated automatically when a new event happens.
So my problem is... one of my coworkers is visually impaired, so I've implemented a way so that when he select a fragment of text, that fragment of text is read loud on speakers... up to here everything is working... but if while he is selecting a text a new event happens and the log is updated, the selected text sometimes is lost or is messed up.
Is there a way to detect when he is selecting text? So I can't stop updating while he select text. (And I don't mean to check if the selected text lenght is higher than 0, because sometimes I'd want to keep text selected and neither I want to check if mouse is down because sometimes he uses shift key + arrows to select text)
like Mangist has suggested, you can use the SelectionChanged event and "pause" the updating of the textbox.
or
before you update the textbox, you could run a simple check. for example
if (richTextBox1.SelectedText.Equals(string.Empty))
{
update
}
else
{
dont update
}

textbox with textchanged event firing

I have two textboxes with textchanged events. When I enter characters in first textbox and press enter, the textbox1_textchanged event is fired. Then I enter some characters in the second textbox and press enter, the textbox2_textchanged event fires as well. However, when I move my cursor in first text box without changing any characters and pressed enter, the events won't fire for both textboxes.
I want the respective textbox events to fire without changing any characters in textboxes.
If I press enter with some characters in textbox1/textbox2 its txt_changed events have to fire.
I have tried, but if I enter characters in textbox2 first the textbox1_textchanged get fired.
Any help?
Since you haven't tagged with a webforms/mvc tag I'm going to go ahead and assume this is a WinForms question, in which case look at:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox%28v=VS.100%29.aspx
See the "Events" section for a list of all the events a textbox supports.
My guess is you'll want to focus on Enter and Leave events, as well as your text_changed events that you already have.
I'm also assuming (I'm assuming a lot since you've specified little) that you have the latest version of .NET at hand and not something from the dark ages, so I've pointed you at the .NET 4.0 reference.

C# 2 controls, both need to catch diffrent keypresses, even while one has no focus

I'm trying to make some sort of intellisense look a like for an editor I'm working on.
When the user types in the richtextbox and the letters match a word a listview will pop-up and the matched word will be selected.
the problem is the focus is still on the richtextbox so all the key presses will go to that control, which is good, except that if there's a match I need the Return and arrow keys to trigger in the listview and not in the richtextbox.
is there some way of using the keypress event of the richtextbox to send those keys to the listview?
thanks
Would it not be easier to use scintilla, the .NET wrapper can be found here, which would do all of what you are looking for?
Edit: It looks like someone has done this, in what you are trying to achieve, an article about it is on CodeProject.
I think it is good that the focus remains on the richtextbox.
How about subclassing that listview to add methods like void SelectNext(), void SelectPrevious(), and string PerformCompletion(). You handle mouse events only for the richtextbox. If the textbox is visible, and the user presses a down arrow, call SelectNext() and swallow the keypress, but if it is not visible, just navigate down to the next line (let the keypress through).
If the listview is visible and the user presses Enter or Tab or . or whatever, call PerformCompletion() to get the selected string, then add whatever characters haven't already been entered.

C# Auto Clearing Winform Textbox

I have a user that want to be able to select a textbox and have the current text selected so that he doesn't have to highlight it all in order to change the contents.
The contents need to be handle when enter is pushed. That part I think I have figured out but any suggestions would be welcome.
The part I need help with is that once enter has been pushed, any entry into the textbox should clear the contents again.
Edit: The textbox controls an piece of RF hardware. What the user wants to be able to do is enter a setting and press enter. The setting is sent to the hardware. Without doing anything else the user wants to be able to type in a new setting and press enter again.
Hook into the KeyPress event on the TextBox, and when it encounters the Enter key, run your hardware setting code, and then highlight the full text of the textbox again (see below) - Windows will take care of clearing the text with the next keystroke for you.
TextBox1.Select(0, TextBox1.Text.Length);
OK, are you sure that is wise? I am picturing two scenarios here:
There is a default button on the form, which is "clicked" when enter is pushed".
There is no default button, and you want the user to have to press enter, regardless.
Both of these raise the same questions:
Is there any validation that is taking place on the text?
Why not create a user control to encapsulate this logic?
If you know the enter button is being pushed and consumed fine, how are you having problems with TextBoxName.Text = string.Empty ?
Also, as a polite note, can you please try and break up your question a bit? One big block is a bit of a pain to read..

Categories

Resources