Pass CancelEventArgs to custom Validation verification event

Lance James 371 Reputation points
2021-04-27T03:23:04.177+00:00

I am using a KeyUp event looking for a tab key entry. Once the tab key is detected I need to validate the input.

The purpose for this is that the KeyUp event needs to Enable and make Visible buttons on the form if the validation is successful. I have errorproviders on each textbox. I can pass the form and the errorprovider to the validation class method. However, I cannot find how to pass the correct CancelEventArg to the method so I can set it's value to true or false for the textbox that is being validated. This needs to be able to cancel the event to move to another field and I will follow-up with clearing of the error so the errorprovider loses the icon.

Trying to replicate this:

private void txtTempEmpID_Validating(Object sender, CancelEventArgs e)

The Validating Event for a textbox has this as a received parameter. However, I can't use this event as it does not recognize my request to make visible or enabled buttons as it is trapped in the validating because there are no other enabled/visible controls that can take focus. Hence, the event can't fire.

Long story short: Present a textbox awaiting a scan of an employee's badge. Once the scan is made (String collected, tab key entry. all one event) I need to make the appropriate Buttons appear on the form for that employee.

Developer technologies | C#
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,656 Reputation points
    2021-04-27T05:59:24.147+00:00

    Hi LanceJames-3930,
    Some key presses, such as the TAB, RETURN, ESC are typically ignored by some controls because they are not considered input key presses.
    You can handle those key strokes via PreviewKeyDown event.
    Please try the following code:

    private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)  
    {  
        if (e.KeyData == Keys.Tab)  
        {  
            button1.Visible = false;  
            e.IsInputKey = true;  
        }  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Lance James 371 Reputation points
    2021-04-27T16:22:23.823+00:00

    Thanks for the input.

    I am using a KeyUp Event to detect the tab keystroke. It is here I need to start the validation.

    This is the built-in Validating Event as I have it written. This is just for testing.

    private void txtEmpID_Validating(object sender, CancelEventArgs e)
            {
                string error = null;
    
                if (txtEmpID.Text != "L")
                {
                    error = "Validation failed - Employee ID";
                    e.Cancel = true;
                    errorProvider1.SetError((Control)sender, error);
                }
            }
    

    The Event is added automatically here in the Form.designer

    this.txtEmpID.Validating += new System.ComponentModel.CancelEventHandler(this.txtEmpID_Validating);
    

    I need to pass the CancelEventArgs e to my custom Validating method as I will be removing the built-in Event. How do I get the name of the Object that is sending the CancelEventArgs to the built-in Validating Event?

    Lance

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.