Hold focus on textbox that do something in the textchanged event - c#

I have a problem:
I handled textchanged event of a textbox. When the event fires I do something in the UI (like add a row in a listbox) and the textbox lost the KeyboardFocus.
How can I hold the KeyboardFocus on that textbox?
Thanks

Write the following
this.MyTextBox.Focus()
at the end of your handler.
or make your UIElement.Focusable = false. it should help.

You can set the focus back on the textbox in the code-behind where you handle the event using textBoxName.Focus();
Focus can be set in the XAML using
FocusManager.FocusedElement="{Binding ElementName=textBoxName}"
but if the focus is lost when you add a new element then you may need to use code-behind to reset it.

Related

ListView LostFocus Event isn't working fine

I've a listview and a textBLOCK inside it. I want to collapse the visibility of listview when I tap anywhere else on my screen. I tried to do this using LostFocus Event for my listview but it is fired ONLY when I selected an item. Why does it behave like that?
Thanks in advance!
The only way to do this as of right now, based on this seems to be to hook into the touch input directly from the CoreWindow and then each time compare the point on the touch input with the bounds of the ListView relative to the CoreWindow.
The WinRTXamlToolkit may assist in this by adding such extensions as GetBoundingRect to FrameworkElements.
I used the IsTabStop Property. Actually i have a user control in in which i'm having this Listview. I did the following steps:
I set the IsTabStop property of UserControl to "True".
Then I set the focus to the textblock when it is tapped by doing:
this.Focus(FocusState.Pointer);
Then created the LostFocus Event of my UserControl and Collapsed the visibility of ListView.
Try using TextBlock's Leave or LostFocus event because if you focus the TextBlock, the ListView losts focus but if you focus TextBlock when you focused something out of ListView, the ListView never becomes focus.

Can I fire an Event when I tab out of a textbox without entering a value?

I have an ASP.NET Webform (C#) that has a textbox on it. I would like to know if there is a way to fire an event when the user tabs off the textbox without entering a value?
I set Autopostback=True and CauseValidation=true. If i enter anything in the textbox it fires the TextChanged event which is fine. But if they tab without entering anything nothing happens.
Any Ideas?
thank you
You csn handle Lost focus event on client side (js), then cause postback or run ajax request if you need server side code.
There is an event called OnBlur in which you can use when the user doesn't enter any value.
Textbox OnBlur event fires when user leaves the textbox.
Use something like this.
in OnBlur add a javascript function as per your requirement.

Why Text_Changed event of TextBox does not fire when we change its text programmatically?

I have designed a softkeypad and changing textbox text programmatically such as
myKeypad.getControl.Text += "Char";
but textbox textchanged event does not fire.
How can I solve this problem?
Is it possible that we can create custom textChanged and KeyPressed Events which can fire by changing text or key press by softkeypad programmatically?
It should be triggered.
From MSDN Control.TextChanged Event:
This event is raised if the Text property is changed by either a programmatic modification or user interaction.
Where are you adding the Event Handler?

What Textbox event is raised right after bound data source is updated?

In windows forms, when I tab out of a text box, the bound data source value is updated. I'd like to capture the events right before and right after the data source changed. I think the OnLeave event is what I want for the before event. In the debugger, I'm not seeing the data source value changed. But, what event can I key off of for the after event?
I don't think there is an event that does exactly what you're asking, the closest I think you're going to get is to use the DataBindings and find your specific Binding and the you can catch the Parse event. But I believe this event fires before the data is pushed back to the source, so it's not much better than the LostFocus event.
The default event for TextBox DataBindings is DataSourceUpdateMode.OnValidation. When you tab out of the TextBox, the following events will fire:
Leave
Validating
(data source gets updated)
Validated
The Validating event has a CancelEventArgs parameter that allows you to cancel the leave attempt for the TextBox (the focus will remain in the TextBox).
If you use DataSourceUpdateMode.OnPropertyChanged, it will update the data source with every keystroke or text change.

TextBox TextChanged event does not fire when Visible = False?

I have a textbox bound to a datasource. The textbox's TextChanged event updates another textbox.
The problem is, I do not want the first textbox to show, so I set its Visible property to false.
However, now the TextChanged event does not fire!
I can work around it by setting Visible=True, Left=-100000 on form load, but I'd like a proper solution.
Can anyone offer an explanation?
Set your textbox.Visible = false in the FormLoad event instead of in the designer. It has to do with handle creation. If the textbox is not visible during construction, then the handle is not created. If the textbox is made invisible after construction, then the handle will have been created and events will occur.
See this discussion on MSDN.
An alternative solution to the accepted answer is to set up the TextChanged listener on Loaded, this works for me just the same (in Silverlight at least) and keeps the designer view as it should be.
What type of datasource is it? It might have an event that you can use directly instead of using a textbox to listen for an update.
If Visible is equal to false then the Control is not rendered. Therefore it will be unable to fire an event.
Instead, set the style to display:none. You can set/unset this programmatically using the Attributes collection:
MyTextBox.Attributes.Add("style", "display: none");

Categories