question

LanceJames-3930 avatar image
0 Votes"
LanceJames-3930 asked LanceJames-3930 commented

Pass CancelEventArgs to custom Validation verification event

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.

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


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.

Maybe it is possible to handle the Changed event of textbox or even check the text periodically using a timer? This seems easier.


0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered LanceJames-3930 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I took your approach. Following MS guidance I put the logic in the KeyDown event. Everything works has planned.

Thank you for you help.

Respects,
Lance

0 Votes 0 ·
LanceJames-3930 avatar image
0 Votes"
LanceJames-3930 answered LanceJames-3930 published

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.