Detect when user is selecting text in RichTextBox - c#

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
}

Related

Can you make a text box on a C# form act like an excel box?

I want the user to be able to type like = or whatever and then write an equation. Once they click off the box, I want the textbox to show the answer.
For example they could put =2+2 and it would display 4 when not selected.
I have no idea how to do this at all or even where to begin. I can get the textbox to calculate it somewhere else but not in the box itself where the equation is written or without having the user press a button.
What you're probably looking for is the LostFocus event. This event fires whenever a control loses focus (for example when a user tabs out of a text box). If you listen on this event for your text box, you can then call whatever processing code you want to handle any calculations required by the value of the text box and then set its value to the result of the calculation.

Highlight certain controls when hovering above a button

I have the following case:
There are several list boxes where the user can select something, some text boxes to enter text and several buttons to execute commands.
If a button is not enabled (due to a wrong/missing selection in a list box and/or a wrong entry in a text field), I'd like to show the red error adorner around the elements that needs to be fixed.
I know how I can show the red border when a entry is not correct using Validation rules - but they are not applicable since the fact "is correct" depends on the command the user wants to execute. E.g. to add an element, there is no need for a selected element in a listbox, but if you want to delete one, there needs to be a selected one.
You should use an attached behavior on the button itself. Then, hook the MouseEnter and MouseLeave events for the button.
Then all you have to do (when the mouse enters and exits the button) is fill out the validation rules for each control you want to "turn on or off" for the validation box.

How to Insert the Cursor at the Beginning of Text in a Textbox Using WPF

I have a listbox which, when an item is selected, is used to populate a set of controls such as textboxes, radio buttons, and the like. What I want to do is cause the cursor to appear in the first textbox after the selected item is parsed into the respective controls. After spending time reading a number of posts here and researching on MSDN, I am still unable to accomplish this simple task.
In the code, I have txtInstName.Focus();. I have confirmed by checking the Keyboard.FocusedElement property that txtInstName does in fact have the focus. So how do I put the cursor at the beginning of the text in txtInstName? I've tried txtIns6tName.Select(0,0); but that does not insert the cursor where I want it.
Any ideas?
You can use the following property
MyTextBox.CaretIndex = someInt32;
this property gets or sets the insertion position index of the caret.

c# combobox drop down click deletes everything in textbox

I have a windows program (.NET 4.5) that has a combobox with custom dropdown data. My autocomplete type is set to AppendSuggest, so that the customer can easily type in a name, press tab or enter, and then move on to the next box to fill in.
When the combobox loses focus (goes to another control), the program will parse the text in the textbox. If the name exists in the database, it will attach that person to this instance, otherwise it will create a new person with the name (properly cased) in the database.
Pressing tab allows the autocomplete box to fill in the remaining text and it successfully attaches it to the database. However, if the enter button is pressed or, more importantly, the customer tries to click on a user from the autocomplete drop down list, the textbox simply deletes all text in it and moves on to the next box with nothing in it.
I can't find out how to intercept the data that the user clicks. Ideally, I just want the user to click an item from the dropdown list and have it fill the text in, then parse that text and move on.
I have also checked my code and there is no where in the code that deletes the text from this combobox. It seems to be happening by itself behind the scenes.
Also, I am a somewhat advanced programmer in C#, and although I really haven't worked with autocomplete, I am baffled that I am stumped on something as simple as this.

Which event captures every edit to a RichTextBox?

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

Categories

Resources