Resumen Problema con validación en LostFocus

Las validaciones es aconsejable realizarlas en el boton de aceptar, pero
si lo que deseas es validar con el LostFocus aqui tienes un articulo de
Microsoft que solventa el problema.

Saludos
Salvador Marine
Barcelona España

Validating Text Box Data Causes Extra LostFocus Events
Last reviewed: June 21, 1995
Article ID: Q96846 The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0 -
Standard and Professional Editions of Microsoft Visual Basic for
  MS-DOS, version 1.0
SUMMARY
Using the LostFocus event to validate data in a text box can cause
excess LostFocus events after the data is determined invalid and focus
is set back to the text box. Setting the focus back to the text box, as
is the custom when data is invalid, causes a LostFocus event to occur in
the control that just received the focus. If that control is also
validating data in its LostFocus event and no data (or invalid data) is
entered, that control could set the focus back to itself, triggering a
LostFocus event in the text box.
MORE INFORMATION
To work around the problem, you need to handle the intended LostFocus
event and ignore those generated as a side-effect of handling invalid
data. Using a Dim Shared variable in Visual Basic for Windows or Visual
Basic for MS-DOS, you can use the LostFocus event to validate text box
data. A Dim Shared variable holding either the TabIndex of the next
control to be validated or a flag indicating that any control can be
validated next, allows you to ignore unintended LostFocus events in
other controls.
The example below demonstrates how to use a Dim Shared variable to
validate Text box data in the LostFocus event. The example gives
step-by-step instructions for Visual Basic for Windows, but you can use
the exact same code and controls in Visual Basic for MS-DOS without
modification.
Steps to Create Example
1. Start Visual Basic or from the File menu, choose New Project (ALT, F,
N)
   if Visual Basic is already running. Form1 is created by default.
Add two text boxes (Text1 and Text1) to Form1.
Add the following code to the General Declarations section of Form1. (In
Visual Basic for MS-DOS, add the code to the form-level code.)
Dim Shared Focus As Integer
Function IsValid (t1 As TextBox) As Integer
      If t1.Text = "" Then

IsValid = False Else ' add other data restrictions here IsValid = True
End If End Function
Add the following code to the Form_Load event procedure of Form1:
   Sub Form_Load ()
      Focus = -1
   End Sub
Add the following code to the Text1_LostFocus event procedure:
   Sub Text1_LostFocus ()
      If Not IsValid(Text1) And (Focus = -1 Or Focus = Text1.TabIndex)
Then
         MsgBox "Text in Text1 invalid"
         Focus = Text1.TabIndex
         Text1.SetFocus
      Else
          Focus = -1
      End If
   End Sub
Add the following code to the Text2_LostFocus event procedure:
   Sub Text2_LostFocus ()
      If Not IsValid(Text2) And (Focus = -1 Or Focus = Text2.TabIndex)
Then
         MsgBox "Text in Text2 invalid"
         Focus = Text2.TabIndex
         Text2.SetFocus
      Else
         Focus = -1
      End If
   End Sub
>From the Run menu, choose Start (ALT, R, S) to run the program. Text
boxes Text1 and Text2 both contain the default text, their Name
property.
Delete the text in Text1.
Press the Tab key to move the focus to Text2. The Text1_LostFocus event
detects that there is no text in the text box, displays a message box
stating that the text in the Text1 box is invalid, and sets the focus
back to the Text1 box.


> -----Mensaje original-----
> De:    Perez Hormahechea, Ernesto [SMTP:eph-dossa-cadiz@DRAGADOS.com]
> Enviado el:    miércoles 10 de diciembre de 1997 11:38
> Para:    'Lista de VB'
> Asunto:    (VB-ESP) Problema con LostFocus
>
> Hola a todo/as!
>
> Tengo un pequenno problema que me genera el tipico bucle eterno...
>
> Tengo un formulario con varios textbox que el usuario debe teclear. En
> el evento KeyPress de los textbox controlo que solo se introduzcan
> numeros o letras... En algunos de esos textbox (principalmente los de
> fecha y dni) valido que sea correcto el campo y lo formateo en salida:
> eso lo hago en el LostFocus mas o menos asi:
>
> Sub Lost_Focus
>     Si el foco esta en un boton de cancelar
>         salgo
>     end if
>     Valido el campo
>     Si hay error
>         Mensaje de aviso
>         Foco en el campo erroneo
>     else
>         saco el campo formateado (p.e.: 12/12/1997 o 32.345.456)
>     end if
> end sub
>
> En principio parece todo correcto pero... si paso de un campo que
> valida
> en el lostfocus a otro campo con validacion... Se enbucla por que en
> el
> primero da error y el segundo vuelve a perder el foco y da error, y el
> campo vuelve a perder el foco y valida y da error... etc...
>
> Como arreglarlo? Teoricamente las validacione debo hacerlas en el
> lostfocus...
>
> Un saludo desde Cai,
> Ernesto



Resumen Resumen

Visual Basic Página de Visual Basic

Página principal Página principal

www.jrubi.com