Validating Event handler doesn't allow Cancel of Form

R

Royce Fickling

Hi,

I have implemented a form using a TabControl with 5
TabPages. On one page, I have a number of controls, i.e.,
ComboBoxes, RadioButtons, NumericUpDowns, etc. I want to
validate the data entered by the user into these controls
and at the same time allow the user to cancel the form if
he wishes. To implement this, I copied some sample code:

private void tabControl1_SelectedIndexChanged(object
sender, System.EventArgs e)
{
tabControl1.TabPages
[tabControl1.SelectedIndex].Focus();
tabControl1.TabPages
[tabControl1.SelectedIndex].CausesValidation = true;
}

private void tpSchedule_Validating(object sender,
CancelEventArgs e)
{
bool bValid = true;
if ((string)cboSchedule.SelectedItem == "Weekly")
{
if (cboRunOnDOW.SelectedItem.ToString()
== "")
bValid = false;
}
}

My problem is that the TabPage always has the focus and
won't allow the Cancel button push to be handled. If I
remove the first line of the SelectedIndexChanged() event
handler, the Cancel button push is handled, but then the
validation stuff is never invoked. Does anyone know how to
fix this?

Thanks,
Royce
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

Safety first - have you ensured the Cancel button has its CausesValidation
property set to False?
 

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