TextBox - Can I keep the selection highlight when it loses focus? - c#

I would like to have a regular TextBox on my form, where the selected text is still highlighted even if you use another control, e.g. push a button.
Does anyone know a way to achieve this (without using a RichTextBox which is not suitable for what I am doing).

Sounds like you are looking for the HideSelection property:
Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.

The HideSelection property is your friend. Set it to false and you should get what you are looking for. I never quite understood why the default is true.

Related

Set textbox as focused element

I am trying to set focus on a text box. I tried set focus for cursor
but all that does is put the cursor there and freezes it. You can't actually type anything still. You end up still having to click the text box to type. I did exactly as they did in that question. I read some stuff about logical focus and physical focus? Not quite sure how to go about doing what I need. Do I need to create an attached property and handle it that way? I am trying to avoid code behind at all costs.
Also, after clicking a "Submit" button I would like to have it set focus back to the text box again
Here is the code I tried, the FocusManager is on a grid that wraps the textbox:
FocusManager.FocusedElement="{Binding ElementName=FocusedTextBox}"
<TextBox Text="{Binding serialNumber}"
x:Name="FocusedTextBox">
</TextBox>
As I said, all this did is put the cursor there and you can't type anything until you again click in the text box. Any suggestions?
Thanks,
Just give the textbox a name (e.g. "theTextBox') and call theTextBox.Focus().
If you're trying to do this in MVVM with data-binding (and you should be) then you can use an attached property to bind to a property in your view model.

How to disable focus on textbox on load in a view in WinRT?

I have a TextBox in WinRT which got the focus (Pointer focus) on load in a view in WinRT. I want to disable this and change the focus to unfocused at load. When I am in constructor of the view, the TextBox is Unfocused, but when I reach the event Loaded, the TextBox got, I don't know why, automatically, the focus at Pointer.
No instruction are done to put the focus on this control. I don't understand why it got the focus.
I try to change TabIndex, no success, the control got the focus again. When I try to put manually the focus on unfocused, I have an exception: "Value does not fall within the expected range.". I don't understand why I have this exception. I have only one control with the name that I gave to him.
Thank's for reply.
Solution 1:
You can set the textbox TabStop property to false.
Solution 2:
Set another controls TabIndex to a lower value than the TabIndex value of the texbox (the control you wish to recieve focus on startup).

Understand textbox.Select()

I am trying to understand a piece of code in order to refactor it. There are several verifications for the input value to check if they are valid, and on each verification there is a line of code that I do not understand what it does.Here is the code:
if (IsNotDouble(weight))
{
MessageBox.Show("Weight must be a numeric value!");
txtWeight.Select();
return;
}
txtWeight is a textbox.
Can anyone tell me what txtWeight.Select() does here.I can not understand why this piece of code should be posted here after each time an error is thrown.
It sets the cursor into the textbox where you have to enter the weight.
The TextBox.Select() method from MSDN.
Activates the control. The Select method activates the control if the control's Selectable style bit is set to true in ControlStyles, it is contained in another control, and all its parent controls are both visible and enabled.
In your case, it seems whenever a validation check has failed, the particular text box is selected to activate it in order to set the visual focus to it.
According to the MSDN:
The Select method activates the control if the control's Selectable style bit is set to true in ControlStyles.
It means, that Select sets focus to the Control so in your scenario if IsNotDouble(weight) is true, you set focus to txtWeight so that user can write there a text immediately without seeking the txtWeight through the entire form.
Select method activates the textbox control or you can think this as bringing the focus to the textbox.
It may not be required in your case as when the verification is happpening most probably the focus is already on that textbox

How to make that textbox lost focus in WPF?

I have textBox.I begin write something in textbox,and it opens popup with listBox.When i select any item from listBox,textBox losts focus.How to make that listbox not to take focus,or how to get focused textbox?I use MVVM
The simplest way would be just to set focus manually to the textbox whenever it loses the focus. Mind however that this may automatically close your popup, so you'll need to tweak it as well.
By the way, maybe you just need a combobox instead? It's exactly a textbox with a drop-down list of choices.
(MVVM doesn't matter here, the problem is purely in the view.)

Is there a way to have a richtextbox display the highlight when it loses focus?

I have a richtextbox, when I leave it for example to go to another panel where I want to manipulate the selected text I can no longer see the selected text. Is there a way to make it still show the highlight?
Set the RichTextBox's HideSelection property to false.

Categories