Windows Forms Error Provider does not display in custom tab control

898 Views Asked by At

I'm trying to build a Wizard framework in Windows Forms. I've managed to glean a lot of useful tips from this and other sites which have gotten me very close to success. However, I'm having a problem with displaying an ErrorProvider on any tab page other than the first page of the wizard.

My Wizard control is a UserControl. It contains a custom tab control that I've derived from TabControl so that I can hide tabs and ignore attempts to navigate between tabs using keypresses, along with the usual collection of Back/Next/Finish/Cancel buttons at the bottom of the control.

I've used reflection to allow me to raise the validation events on a particular TabPage that belongs to the Wizard Control when I hit the Next button. (I don't want to validate the whole TabControl, only the currently active page.) When I do this, I see in the debugger that my Validating routine for the controls on the current tab page is correctly called and I see that I've called the ErrorProvider that I've attached to the particular control (a TextBox in this case) with a valid error message. I set Cancel to true for the CancelEventArgs in the validating routine and that's picked up by the code that uses the reflection mechanism so that I see that I've failed and don't change tabs. And I set the focus successfully to the control that failed validation.

So all that appears to be working just fine.

Unfortunately, I don't see the ErrorProvider's cheery blinking icon unless I'm on the first tab page. For all of the other tab pages, there's no message visible at all.

I'm baffled. Any thoughts? I can provide code snippets, if helpful.

Thanks!

1

There are 1 best solutions below

0
On

I assume that in your case the button that moves to the next step of the wizard is placed outside (below) the TabControl

I noticed that the icon is displayed correctly if I pressed the button without releasing the mouse button. It seems that the button outside the container gets focused event though a validation error has occurred (normally you would not be able to leave the active control).

I worked around this issue by registering an event handler for the buttons MouseUp event to "refocus" the TabControl:

private void cmdOK_MouseUp(object sender, MouseEventArgs e)
{
    tabControl1.Focus();
}

Note: you also need to set your forms ActiveControl property the one of the controls that failed validation.