Set textbox as focused element - c#

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.

Related

WPF, How do I see what I'm doing in the editor after using Bindings?

It's my first time using WPF, I'm still getting used to the controls. I have a couple of textboxes, each having a Binding to a string somewhere. Works great when running the program, but in the editor.. all I see is blank emptyness. When I mouse-over I can see the border of the textbox, but having to mouseover to find all my elements in my program.. well, that's not a nice way to design a layout.
Is there any way to have 'default' text visible in the editor? Some parameter I'm missing, some border I can activate only during editing mode, heck, -any- way I can see what I'm doing without influencing what the program looks like when being ran?
//Properly binds and displays text, but invisible in the editor.
<TextBlock Height="48" Margin="26, 191, 0, 0" Text="{Binding FirstTextBox} Width="175">
ANSWER::
After hearing the keyword 'Design-Time', How to set a value to display for bound properties in WPF design mode? brought me to the proper answer.
you can set DataContext to your Model. If you want to see your property value in editor.
Best Regards
Did you try by using FallbackValue?
Here is an example from other post to set a default text visible as you asked.
WPF Binding - Default value for empty string

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.)

C# How to commit a TextBox?

In a form, I have a TextBox Binding an Object on its member property "Title". Along with it is a "Save" button to test the binding.
Seems like the underlying object property does not get updated unless the textbox loses focus. But there no form.ActiveControl.Blur() for use. Besides, this does not seem like a sound hack.
Anyway to do this better? Thanks.
EDIT: Sorry for not being clear. My question is in the title: "How to commit a TextBox". I use the term "commit" from the DataGridView commit and BindingSource commit. And it's in WinForms. (Have never worked with WPF, so it didn't occur to me. Sorry).
The actual scenario I have is I have a bunch of TextBox binded to property of a single Object. The user enters values in all the TextBox and when the user clicks save (toolbar button), the last TextBox is still in focus (or in editing mode) hence the save will not capture the last value in the last textbox.
I want to find the correct way to "commit" the textbox value just before saving.
Thanks.
Since the question has been updated to indicate this is WinForms, you'll need to handle things a little differently than if this were a WPF application. Fortunately, it turns out that the solution is very simple.
Whenever the user clicks on the "Save" button (so, say, in your Save button's Click event handler), you need to call the EndEdit method on your BindingSource. This will cause all pending changes to be committed to the underlying data source, exactly what you were hoping to accomplish.
Also see the relevant documentation on MSDN for more details.
Sounds like WPF from the problem description..
You want to change the binding so that it updates when the property value changes instead of when the textbox loses focus (which is the default when binding to TextBox.Text). You can do this by setting the UpdateSourceTrigger property on your binding:
<TextBox Text="{Binding UpdateSourceTrigger=PropertyChanged}"/>

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

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.

How can I prevent a Checkbox state switch when the user clicks on the text?

I have a list box with Checkboxes in it. I want to prevent the Checkbox from changing its status if the user clicks on the text next to it. I only want it to change if the small box is clicked directly.
Is there any way to do this in windows forms?
Greetings and thanks in advance.
Place the text next to it in a Label, instead of the Text property of the Checkbox. Or you could create your own control which has a Checkbox and a Label. The Text property of the control would then fill the Text in the Label, and you could expose all of the Checkboxes regular properties in your control.
That's fairly non-standard behavior. Users are going to expect to be able to change the checkbox when clicking on its label, and are going to be frustrated, confused, and surprised when it doesn't work. I'd recommend not doing this. I'm not the only one.
(Yes, it's about web design, but many of the concepts are applicable in desktop application design as well.)
You could always not fill in the Text property of the Checkbox and make a completely separate Label control.
Otherwise, you will probably have to do explicit hit testing within the control to see if they hit the box or text. And then you will have to worry about checking the margins, which side the box is on, and other things that can change the position of the box.
I personally was only able to freeze things.
I freeze the check boxes by handling the Click and ItemChecked events,
and change the check state back, when it gets modified.
I use a menu to check/uncheck items and let user decide to use the menu or classic behave.
Cheers, good luck.

Categories