setting focus to control after validation

  • Thread starter Thread starter kelvin.koogan
  • Start date Start date
K

kelvin.koogan

I have a number of controls on a tab page. I want to validate them
all
when the user tries to leave the tab. I then want to highlight the
first control which fails validation. How can I do this? I try
setting
focus but something seems to set the focus back to the control which
had it before validation. Any ideas?

TIA,
KK
 
Hi Kev,

Are you using or tried the ErrorProvider control? (find it in the
"Components" toolbox, or "All Windows Forms" toolbox) This offers - by
default - a red circle "blob" + exclamation mark that will blink for a few
seconds and provide a tooltip of the error against any control you want to
have it. E.g. textboxes, list controls, etc. Remains invisible when not
set.

e.g. to set:
me.err.seterror(me.mytextbox, "My error text")

To clear a single instance:
me.err.seterror(me.mytextbox, "")

Validation can be done either on each control's change event (which I
commonly use, but it does depend on what you're trying to achieve), or after
a single large event, such as your change of tab page. I would suspect you
would need to ensure that you re-select the tab page (if you have a set of
more than one tab anyway), so that the relevant tab is topmost and thus
visible to the user, then you would set the focus to whichever control on
the tab you want.

Hope that helps.

Al
 
Alec said:
Hi Kev,

Are you using or tried the ErrorProvider control?

Good suggestion, thanks.

However I'm having a problem with the ErrorProvider (in C++, VS2005).
If the ErrorProvider is highlighting an error then it is impossible to
change a checkbox or dropdown a dropdownlist. This includes if these
controls are the ones with the errors. So for example if the user
needs to select something different from a dropdownlist they can not
because the dropdownlist is opened and then immediately closes again.

Any ideas what is wrong?

TIA,
KK
 
Hi Kelvin,

I haven't experienced that issue before - though I'm using VB.NET rather
than C++. I wouldn't have thought it would make such a behaviour difference
occur though. I don't have C++ installed either, so can't experiment.

However, there is an MSDN example of using the ErrorProvider that includes a
C++ version. It's for VS 2003, but usage shouldn't be much, if any,
different:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.errorprovider(VS.71).aspx
Maybe that can help you with the next step?

Also a search on "c++ errorprovider" turns up quite a bit in Google.

Al
 
- Show quoted text -

It seems I've stumbled across a combination of factors which cause
this problem.

I have a tab control. I'm performing validation on the TabPages where
I valid all the controls on the page. I put up a MessageBox to tell
the user to fix the errors.

This combination shows the problem. If I don't put up the MessageBo
then it works OK.

Any idea why this might be?

TIA,
KK
 
Possibly due to the messagebox taking focus as it's a modal object.

Are you setting the focus of the first error-state control before displaying
the messagebox, or after displaying the messagebox?

Al
 
Back
Top