Windows form application - text Highlighting - c#

I am using a WFA which has a ToolStripMenuItem that is supposed to search for a word in a textBox and highlight it. This is the highlighting code.
first = first index to be highlighted
, length= number of characters to be highlighted.
textBox2.SelectionStart = first;
textBox2.SelectionLength = length;
//scroll to the caret
textBox2.ScrollToCaret();
everything seems to be working well. However, If the mouse cursor isn't inside textBox2 (say in another textBox). The highlighting doesn't work. Is there a way to make it work regardless of the position of the mouse cursor? in other words, is there a way to change my code to make it highlight the text in textBox2 even if the mouse cursor wasn't inside textBox2 before hitting the ToolStripMenuItem?
P.S. It's worth mentioning that I am using c# in VS 2010

Try:
textBox2.Focus();
before you run the code that highlights the text. This will move input focus (and your cursor) to the textbox

just use richtextbox and change the backcolor of the text.
this way you can use multiple highlights at a time and the highlight won't be lost after the user focus the textbox by mouseclick. Additionally you can save all postions and automatically select the textpart if the user clicks into the highlighted area.
you can also show the selection like LarsTech mentioned, but after the user clicks inside the textbox the selection will get lost.

Related

Programmatically changing WPF TextBox's Text with regard of cursor position and current selection

I am implementing some sort of NumericKeypad Control for my WPF application which clients can use to conviniently enter text with a Touchscreen.
The Control itself runs fine, I even managed to run the Control (which is a Window) in the foreground while the TextBox, which is the target of the input, retains the focus and selection.
Now, I want to add the user's input into the TextBox's Text property just like if he had entered them with a keyboard. That means that certain behaviors should be adhered to, for instance...
When the cursor is inbetween the Text, the character should be placed to where the cursor currently resides.
When something is selected, the selection should be replaced with the entered character.
Is there anything I can use to do that? The only function I found is TextBoxBase.AppendText, which does not consider the cursor position or selection, but simply appends the entered String to the end of the Text.
Use TextBox.SelectedText, which has both a setter and a getter.
If no text is selected, SelectedText is the zero-length range at the caret position.

Cursor in textbox goes to wherever I click

Working on a site in ASP.NET, I have a textbox, and if I click in the middle of the textbox, when the textbox takes focus the cursor is in the middle, if I click 1/4 in from the left, on focus the cursor goes to 1/4 from the left.
I'm only having this issue with this particular textbox, all the other ones on my page function as expected (i.e., no matter where you click, the cursor defaults to the beginning of the textfield). Odd thing is that all textboxes are defined identically. The only thing that stands out about this one is that it is the last textbox on the page.
Any ideas?
There is no javascript on the page, although if that is the only way out of it I can use it.
Due to the lack of information here, I'm going to take an educated guess. I'm going to assume you are using asp.net Webforms.
Webforms Textbox Control
no matter where you click, the cursor defaults to the beginning of the textfield
This is because when a Textbox is clear of text (including white spaces) the beginning is at index 0.
What I'm thinking is causing your problem is white spaces being entered on that last Textbox control; go to the Properties for the Textbox control and under the Text property, click it and then press CTRL-A to select all characters (including white spaces) and press DEL to delete them all, if any.

Editable TextBlock, Caret Index Query

I am developing a Custom Editable Text Block for my Application. So when the User clicks on the Control, TextBox swaps in for the User to edit the Text. Everything is fine till now, Now my requirement is I want the caret index of TextBox to exactly where the user had clicked on the TextBlock.
So the user won't feel about the swap from UI Point of View.
What approach would be more appropriate? Considering the above factors?!
I am lost in ideas for this thing.
I assume you have a UserControl/Customcontrol with its own Mouse handling support, and a way to tell when to switch the content from TextBlock to TextBox.
In the mousedown event you could store the clicked position, swap to the textbox and in the Loaded event you could use GetCharacterIndexFromPoint to tell which is the position of the click and set the CaretIndex to that position. You might have to adjust your margins if you use any.

Display Cursor in WPF TextBox without Focus

Is there anyway to make it look like a TextBox has focus when it really does not? I would like to display the flashing cursor in a WPF TextBox even though it does not have focus.
Why do I want to do this?
We have a system that accepts touch input from more than one user at a time. One of the users has focus and I cant have it jumping between controls. The secondn user input is from an on-screen keyboard. As they type the letters the text is entered in the textbox. It just looks strange for there not to be a cursor.
Well I think You can use animations for your TextBoxs, it will play all the time or at any time you want without being focused, hope it will help.

RichTextBox text blinking

does someone know, how to make various parts of RichTextBox text to blink in .NET on Winforms?
Or maybe this is already done?(Google gave no help)
Thank you
First you need a timer. after selecting a text and click the blink button save the region of the text (startposition:endposition) to an array. With the timer change every second(for example) the color of the text in regions saved in the array. if the text is changed check if the change occured in a region and inrecrease or shrink the region. if the text is change outside a region check if the blinking text is moved and move the region also backward/foreward.

Categories

Resources