User Control & Tab Pages

S

Sumit

Hi all,

I have a windows form in which i have a tabcontrol having 4 tabs.
I have made 4 user controls for each of the tabpage.
I add the user controls on the tabpage programatically.

Now in the leave event of the user controls i have written a code which
checks whether all the mandatory information have been filled in the
textboxes, comboboxes etc. present on the user control.

Now what i require is that check for mandatory information should only
be fired if i leave the user control by clicking on another tab on the
tabpage. But it should not be fired when i close the windows
form(containing the tabcontrol).
But since i have written the code for checking of mandtaory information
on leave event of the user control, they are being fired in both the
cases i.e.
1. When I make a tab movement(required).
2. When that particular tab containing the user control is active
& i close the windows form. (NOT required).

Is there any way to capture that from where the leave event is being
called.... i.e. due to an active tab being changed or due to the form
getting closed..

Kindly Help !!!
Warm Regards
Sumit
 
B

Bruce Wood

I did this in the following way:

First, I used the Validating event rather than the Leave event. This
also gives you the option of cancelling the user's attempt to leave the
control.

Second, I put the validation code within separate, private methods, so
each Validating event handler calls a private method and, if it returns
false, sets e.Cancel = true to stop the user from leaving the control
(if that's what you want to do).

Third, each one of these private validators checks a flag:

private bool ValidControl1()
{
if (!this.dontValidate)
{
... perform validation...
}
else
{
return true;
}
}

Then, when the user is leaving the form, I set

this.dontValidate = true;

to stop the validation methods from blocking the user's attempt to
leave the form.

Anyway, that was my solution. I've also seen other posts here
indicating how to tell how the user is leaving the form as a whole.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top