سؤال

I have a TextBox. After leaving the textBox the first character should be a capital Letter. Three Events work as same. They are Leave,Validating and Validated.

if (txtLocl.Text.Length > 0)
    txtLocl.Text = txtLocl.Text.Substring(0, 1).ToUpper() + txtLocl.Text.Substring(1); 

Which event of these 3 events should I use?

هل كانت مفيدة؟

المحلول

You can subscribe to the Control.Leave event which will be fired when the control loses focus. Originally, I thought using Control.LostFocus would be the most appropriate event to use but it is not available via the designer meaning you would need to manually subscribe to the event which is a bit ugly and unconventional in my opinion.

private void inputTextBox_Leave(object sender, EventArgs e)
{
    if (inputTextBox.Text != string.Empty)
    {
        string input = inputTextBox.Text;
        inputTextBox.Text = input.First().ToString(CultureInfo.InvariantCulture).ToUpper() +
                            string.Join(string.Empty, input.Skip(1));
    }
}

You sound like you're interested in Control.Validating. The advantage of using Control.Validating is that you can utilize the event handler's given argument; CancelEventArgs and set the Cancel property to true. What this will do is stop the control from losing focus and forcing the user to enter a valid value. I don't think this is appropriate for your application as you are not really validating anything but formatting the input.

private void inputTextBox_Validating(object sender, CancelEventArgs e)
{
    if (inputTextBox.Text == string.Empty)
    {
        statusLabel.Text = "The given input is not valid.";
        e.Cancel = true;
    }
}

Bear in mind that when the form closes, all controls subsequently lose focus and the Control.Validating event is fired which could stop the Form closing until all fields pass their relative validation checks. If you find yourself needing to avoid this behavior a quick search will prevail.

نصائح أخرى

There are many other events also available.

As said by MSDN, When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

1) Enter

2) GotFocus

3) Leave

4) Validating

5) Validated

6) LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

1) Enter

2) GotFocus

3) LostFocus

4) Leave

5) Validating

6) Validated

If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

textBox1_Leave is appropriate for you.

Check the events and description about textboxes over here>>

http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events.aspx

Hope its helpful.

You might want to subscribe to LostKeyboardFocus event (in WPF) or Leave event (in WF).

I'd suggest using the Leave because I assume you aren't validating the value, but formatting it. Validating and Validated should contain code for validation and the aftermath of validation respectively, IMO.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top