Tab control & validation

D

Dennis C. Drumm

Is there a way to prevent the user from leaving a tab on a TabControl until
all the controls on that tab have finished validating?

The problem I am experiencing is when a control fails its validating event
and the user clicks another tab things get messy. Ideally, I think the best
solution is not to leave a tab until the validating event has completed
successfully.

Thanks,

Dennis
 
A

AlexS

Hi, Dennis

did you try to hook TabPage.Leave event? Validating or Validate?
According to MSDN events happen in following order:
Focus events occur in the following order:

1.. Enter
2.. GotFocus
3.. Leave
4.. Validating
5.. Validated
6.. LostFocus
HTH
Alex
 
D

Dennis C. Drumm

Alex

That does not work. My validating event is somewhat unusual. I guess maybe I
should have asked only how to prevent a user from leaving a tab page. Being
able to do that might help for my particular circumstances.

Another approach would be to somehow cancel the pending validating event
when the user selects another tab page. But how can that be done? I can
setup a tab page leave event, but to what benefit?

Thanks

Dennis
 
A

AlexS

Dennis,

I expect that while you are executing code in Leave event your other page
cannot receive focus (even if can, there will be no interaction possible if
you don't call App.DoEvents or Sleep in the code). Hence, if your validation
in Leave fails you can always go back to original page and select / focus
corresponding control. You can combine this with Validating event, where you
can set Cancel=true to prevent further events after Validating.

Did you try to do this?

Generally speaking, I would suggest to reconsider whole validation scheme
you use now - whatever unusual things you are trying to do. If you have
multi-tab entry form final validation should occur only when final tab page
is filled and confirmed. I would allow user to fill pages in the order
he/she prefers and jump between them at will. Otherwise it is better to use
Wizard-style approach, when consecutive screens are shown each after another
and errors on screen N do not allow to go to screen N+1.

HTH
Alex
 
J

Jeffrey Tan[MSFT]

Hi Dennis,

Based on my understanding, you want to validating all the controls on the
tabpage before switch pages.

I think you should hook the Validating event for all the controls on your
form. For certain control, once it has focus, it will validate its data for
closing, leaving, etc.. events.

So the only problem when changing pages is when all the child controls on
the form have no focus, then swith pages will not trigger the child
controls' validating events.

For this issue, I think there are 2 workarounds:

1. loop all the controls on the tabpage, then judge if each control is the
default value(We suspect the default value is not the valid value), if it
is the default value, does not allow the index change.

2. You may add a custom validating type event for each tabpage, then when
registering event handler for child control's Validating event, you should
also register it for the tabpage custom event, then in
TabControl.SelectedIndexChanged event, you can explicit invoke all the
validating event handler, if not valid, then swith back to the orignal
tabpage.

To swith back to the original tabpage, you may use a variable to store the
original page index. Do like this:

int orginal_index=0;
private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Console.WriteLine(this.tabControl1.SelectedIndex.ToString());

if(this.b_switch==true)
{
this.tabControl1.SelectedIndex=orginal_index;
}
orginal_index=this.tabControl1.SelectedIndex;
}

bool b_switch=false;
private void button1_Click(object sender, System.EventArgs e)
{
if(b_switch==true)
{
b_switch=false;
}
else
{
b_switch=true;
}
}

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Dennis C. Drumm

Thanks Jeffrey and Alex:

I really appreciate the helpful comments. That situation is resolved!

Dennis
 

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