Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Consider this simple leave event from a text box control in a visual studio form:
C#
private void dscr_Leave(object sender, EventArgs e)
{
    if (dscr.Text == null)
        MessageBox.Show("You must supply a description");
}

Assume there is a "cancel" button on the screen, and focus is in the "dscr" text box, and it's blank. You click the cancel button. As a beginner, I'm struggling on how to ignore this leave event when a cancel button is clicked.
I've searched quite a while, and cannot find how to accomplish this. The leave event will fire before the click, but there's got to be some way to know the click event is coming, and avoid unnecessary validation.

Can anyone point me to any examples of how to handle this situation, or direct me as to how to accomplish this?
Posted
Updated 2-Apr-23 16:17pm
v2
Comments
Toli Cuturicu 10-Aug-10 10:39am    
This is not an Answer!
It is better anyway to use: string.IsNullOrEmpty(dscr.Text)

personally I would move your


C#
if (dscr.Text == null)
            MessageBox.Show("You must supply a description");


into the next step button e.g. save or update button.
 
Share this answer
 
Comments
Sandeep Mewara 9-Aug-10 12:46pm    
Comment from OP:
Thanks for the input guys. But there is a need to have validation being done prior to a 'save' button, and the example I gave isn't to ask 'where is the best place for validation', but rather how can this be done in C#:

private void dscr_Leave(object sender, EventArgs e)
{
if (a click of this.canbutton is queued up)
return;

if (dscr.Text == null)
MessageBox.Show("You must supply a description");
}

I'm really struggling as to find an example out there of how people do this. Is there any event that can be added to the canbutton that would fire before the leave, where I can set a flag? Is there C# syntax that would tell you what events are queued and set to fire, which can be checked (to replace the psuedo code above)?
Simon_Whale is absolutely right. It is never a good idea to put such a check directly on an edit button because you couldn't even close the form normally without getting the error. The user would be trapped unnecessarily.

It is also more user friendly to give the user some freedom until Next is clicked. It is also nicer to have your checks done in a single function like CheckFormInput() instead of each Leave event of each input control individually.

Good luck!
 
Share this answer
 
Comments
Sandeep Mewara 9-Aug-10 12:46pm    
Comment from OP:
Thanks for the input guys. But there is a need to have validation being done prior to a 'save' button, and the example I gave isn't to ask 'where is the best place for validation', but rather how can this be done in C#:

private void dscr_Leave(object sender, EventArgs e)
{
if (a click of this.canbutton is queued up)
return;

if (dscr.Text == null)
MessageBox.Show("You must supply a description");
}

I'm really struggling as to find an example out there of how people do this. Is there any event that can be added to the canbutton that would fire before the leave, where I can set a flag? Is there C# syntax that would tell you what events are queued and set to fire, which can be checked (to replace the psuedo code above)?
Simon_Whale 10-Aug-10 4:12am    
The validation would be done prior to the save funcationality but under the "save" button, if the validation fails you exit / dont perform the save action.

i.e.

private void dscr_Leave(object sender, EventArgs e)
{
if (a click of this.canbutton is queued up)
return;

if (dscr.Text == null)
{
MessageBox.Show("You must supply a description");
//exit
}
else
{
//Save
}
}
}
E.F. Nijboer 10-Aug-10 16:22pm    
The Leave event simply isn't the place to go. But if you really must have a solution in the Leave event you could check the cursor position and check if it is located within button.
This will give you the mouse position relative to your button, the rest is up to you.

Point p = canbutton.PointToClient(System.Windows.Forms.Control.MousePosition) ;
Another hacky method would be to add two events on canbutton:

1. canbutton_MouseEnter that sets a public variable IsCancelling = true

2. canbutton_MouseLeave that sets a public variable IsCancelling = false

then include it in your check:

if (dscr.Text == null && !IsCancelling)
 
Share this answer
 
Comments
CHill60 3-Apr-23 11:28am    
What if the user hits the Tab key to move to the control? Your events will never fire.
Alternative methods are discussed in Allow Form to close when invalid data is present[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900