开发者

Why is my button's 'Validating' event handler never called?

开发者 https://www.devze.com 2023-02-19 07:45 出处:网络
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.

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.

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 sende开发者_JAVA百科r, CancelEventArgs e )
        {
            System.Diagnostics.Debug.Assert( false ); // we never get here
        }
    }
}

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


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.

From 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:

   Enter    
   GotFocus    
   Leave    
   Validating    
   Validated    
   LostFocus    

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    

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

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());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消