简体   繁体   English

C#中的文本框事件

[英]TextBox Events in C#

I have a TextBox.我有一个文本框。 After leaving the textBox the first character should be a capital Letter.离开 textBox 后,第一个字符应该是大写字母。 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?我应该使用这 3 个事件中的哪个事件?

You can subscribe to the Control.Leave event which will be fired when the control loses focus.您可以订阅Control.Leave事件,该事件将在控件失去焦点时触发。 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.最初,我认为使用Control.LostFocus将是最合适的事件,但它不能通过设计器使用,这意味着您需要手动订阅该事件,这在我看来有点丑陋和非常规。

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 .您听起来好像对Control.Validating感兴趣。 The advantage of using Control.Validating is that you can utilize the event handler's given argument;使用Control.Validating的优点是您可以利用事件处理程序的给定参数; CancelEventArgs and set the Cancel property to true. CancelEventArgs并将Cancel属性设置为 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.请记住,当表单关闭时,所有控件随后都会失去焦点并触发Control.Validating事件,这可能会停止表单关闭,直到所有字段通过其相关验证检查。 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:正如MSDN所说,当您通过使用键盘(TAB、SHIFT+TAB 等)、通过调用 Select 或 SelectNextControl 方法或通过将 ContainerControl.ActiveControl 属性设置为当前窗体来更改焦点时,会发生焦点事件按以下顺序:

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:当您使用鼠标或通过调用 Focus 方法更改焦点时,焦点事件按以下顺序发生:

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.如果 CausesValidation 属性设置为 false,则 Validating 和 Validated 事件将被抑制。

textBox1_Leave is appropriate for you. textBox1_Leave 适合您。

Check the events and description about textboxes over here>>在此处检查有关文本框的事件和描述>>

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

Hope its helpful.希望它有帮助。

您可能想要订阅 LostKeyboardFocus 事件(在 WPF 中)或 Leave 事件(在 WF 中)。

I'd suggest using the Leave because I assume you aren't validating the value, but formatting it.我建议使用 Leave 因为我假设您不是在验证该值,而是对其进行格式化。 Validating and Validated should contain code for validation and the aftermath of validation respectively, IMO. Validating 和 Validated 应分别包含用于验证和验证后果的代码,IMO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM