How to register event for TextBox end editing

1.9k views Asked by At

Super newbie to C# (first day coding), so please don't judge me too much if the following is really a stupid question to you.

But I am looking for a way to register an event for when a TextBox end editing, which is similar textFieldDidEndEditing(_ textField: UITextField) delegate in iOS.

After some googling around, I know how to register an textChanged event with the following:

In .xaml file:

<TextBox TextChanged="textChangedEventHandler"/>

In .cs file:

protected void textChangedEventHandler(object sender, TextChangedEventArgs args)
{

}

I also notice this SO, and finally this documentation by MS and notice this following function:

// This method handles the LostFocus event for textBox1 by setting the  
// dialog's InitialDirectory property to the text in textBox1. 
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
    // ... 
}

But what is not obvious to me is how do I register this event function? Or how do I let the GUI know to call this function when the TextBox end editing?


This is finally what it takes for it to work:

In .xaml file:

<TextBox LostFocus="textFinishedEditingEventHandler"/>

In .cs file:

public void textFinishedEditingEventHandler(object sender, RoutedEventArgs e)
{

}

Thanks to @Dacker!~

1

There are 1 answers

3
Dacker On BEST ANSWER

Normally an event can be registered in two ways:

In markup. In ASP.Net each type of event is exposed with the prefix On, so for Click it's OnClick. In your xaml I don't see the On prefix, so that makes me guess it is the following in your case:

<TextBox LostFocus="textBox1_LostFocus" />

In code behind (.cs)

textBox1.LostFocus += textBox1_LostFocus

If you understand this, you can use better names for textBox1_LostFocus to describe more what will happen instead of when it will happen.