为什么从未调用过按钮的"Validating"事件处理程序?

问题描述:

在下面的微型应用程序中,我想知道为什么从不调用BtnOk_Validating事件处理程序.我希望单击确定"按钮将调用事件处理程序.

In the following mini-app, I am wondering why the BtnOk_Validating event handler is never called. I expected that clicking the Ok button would call the event handler.

实际对话框具有更多控件,每个控件都有一个验证事件处理程序.我的计划是在允许对话框关闭之前,使用确定"按钮验证事件处理程序来调用其他每个事件处理程序.

The real dialog has many more controls, each that have a validating event handler. My plan was to use the Ok button validating event handler to call each of the other event handlers before allowing the dialog to close.

如果不是很明显,那么在Forms开发方面我还是个新手.

If it's not obvious, I'm quite the novice when it comes to Forms development.

using System.ComponentModel;
using System.Windows.Forms;

namespace ConsoleApp
{
    class Program
    {
        static void Main( string[] args )
        {
            Dialog dialog = new Dialog();

            dialog.ShowDialog();
        }
    }

    public class Dialog : Form
    {
        Button m_BtnOk;
        Button m_BtnCancel;

        public Dialog()
        {
            m_BtnOk = new System.Windows.Forms.Button();
            m_BtnCancel = new System.Windows.Forms.Button();

            m_BtnOk.CausesValidation = true;
            m_BtnOk.DialogResult = DialogResult.OK;
            m_BtnOk.Text = "Ok";
            m_BtnOk.Location = new System.Drawing.Point( 0, 0 );
            m_BtnOk.Size = new System.Drawing.Size( 70, 23 );
            m_BtnOk.Validating += new CancelEventHandler( BtnOk_Validating );

            m_BtnCancel.CausesValidation = false;
            m_BtnCancel.DialogResult = DialogResult.Cancel;
            m_BtnCancel.Text = "Cancel";
            m_BtnCancel.Location = new System.Drawing.Point( 0, 30 );
            m_BtnCancel.Size = new System.Drawing.Size( 70, 23 );

            Controls.Add( this.m_BtnOk );
            Controls.Add( this.m_BtnCancel );
        }

        private void BtnOk_Validating( object sender, CancelEventArgs e )
        {
            System.Diagnostics.Debug.Assert( false ); // we never get here
        }
    }
}

请参见我的后续操作问题可以正常工作的更完整示例(

Please see my follow-up question for a more complete example that works (well mostly).

这是因为按钮是唯一的控件,因此永远不会失去焦点.如果添加 TextBox 或可以使按钮成为焦点的内容,则会看到它触发.

Its because the button will never loose focus with it being the only control. If you add a TextBox or something that can take the focus of the button, then you will see it fire.

来自 MSDN >

使用键盘(TAB,SHIFT + TAB等),调用Select或SelectNextControl方法或将ContainerControl.ActiveControl属性设置为当前窗体来更改焦点时,焦点事件会在以下顺序:

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:

   Enter    
   GotFocus    
   Leave    
   Validating    
   Validated    
   LostFocus    

使用鼠标或调用Focus方法更改焦点时,焦点事件将按以下顺序发生:

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

   Enter    
   GotFocus    
   LostFocus    
   Leave    
   Validating    
   Validated    

如果CausesValidation属性设置为false,则将抑制Validating和Validated事件.

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

更新:就像汉斯提到的那样,您需要将在所有其他控件的每个Validating事件中所做的验证提取到单独的函数中.然后,您可以创建一个 ValidateAll 函数来检查所有值.如果函数返回 false ,则您不关闭 Form .如果返回 true ,则调用 this.Close().所以看起来可能像这样:

Update: Like Hans mentions, you'll need to extract the validating you do in each of the Validating events for all the other controls into separate functions. Then you can create a ValidateAll function to check all values. If the function returns false, then you dont close the Form. If it returns true, you call this.Close(). So it might look like this:

// pseudo code
textbox1.Validating += ValidateTx1();
textbox2.Validating += ValidateTx2();
btnOk.Click += OkBtnClicked();

private void OkBtnClicked(...)
{
    if(ValidateAll())
    {
       this.Close();
    }
}

private bool ValidateTx1(...)
{
   DoTx1Validation();
}

private bool ValidateTx2(...)
{
   DoTx2Validation();
}

private bool ValidateAll()
{
   bool is_valid = DoTx1Validation();
   return (is_valid && DoTx2Validation());
}