TabControl.Selecting issue

N

nvx

Hello,
is there any reason why TabControl.Selecting might not fired when I
select a tab different from the current one? There is a DataGridView
on one of TabPages of a TabControl. I need the content of this
DataGridView to be correct before user switches to another TabPage, so
I added my own validating block of code into TabControl_Selecting(...)
method, which in short does this:

private void tabControl_Selecting(object sender,
TabControlCancelEventArgs e)
{
if (!ValidateDataInDGV())
{
e.Cancel = true;
}
}

It works great except one detail: if I insert some "wrong" data into
the DGV and try to switch to another TabPage, it stops the switching
because the data was incorrect. So far it's fine. If I do not click
somewhere into the DataGridView and try to switch to some other
TabPage again, the tabControl_Selecting(...) method is not executed.
Why?

I tried to add inputDGV.Focus(); after e.Cancel = true;, but it didn't
help.

Thanks a lot for any help...

Best regards
nvx
 
P

Patrick Steele

Hello,
is there any reason why TabControl.Selecting might not fired when I
select a tab different from the current one? There is a DataGridView
on one of TabPages of a TabControl. I need the content of this
DataGridView to be correct before user switches to another TabPage, so
I added my own validating block of code into TabControl_Selecting(...)
method, which in short does this:

private void tabControl_Selecting(object sender,
TabControlCancelEventArgs e)
{
if (!ValidateDataInDGV())
{
e.Cancel = true;
}
}

It works great except one detail: if I insert some "wrong" data into
the DGV and try to switch to another TabPage, it stops the switching
because the data was incorrect. So far it's fine. If I do not click
somewhere into the DataGridView and try to switch to some other
TabPage again, the tabControl_Selecting(...) method is not executed.
Why?

I tried to add inputDGV.Focus(); after e.Cancel = true;, but it didn't
help.

Thanks a lot for any help...

Not sure why that is happening, but have you tried capturing the
Deselecting event instead? The Selecting event fires *after* the other
tab has already been deselected. I would think the Deselecting event is
something you want here.
 

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