Big time problem with VB.NET validation

T

Tim Frawley

Source code attached indicates my problem with validation and a button
bar save button.

Fill the Textbox with some text then tab off the control. The message
box will display the text in the textbox from the Validating event.
Now put the focus back on the textbox click the button bar button.
Nothing happens.

The form causes validation is true, so is the button bar and the
control.

This is a big issue for me as some validation code is in the control
and some is in a save validation event.

How do I validate a control that still has the focus to stop my save
validation event?

If I set the focus to some other control the textbox validating event
will fire but it will not stop the save validation.

I certainly do not want to have to code my controls and then put that
same code in my save validation event. Thirty controls on a form will
change your mind about that real fast.

If I attempt to fire a controls Validating event from the save
validation event I do not get a return value so I do not know if the
Validating event succeeded or not.

There has to be a better way! Does anyone out there have any
suggestions?





Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ToolBarButton1 As
System.Windows.Forms.ToolBarButton
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.ToolBar1 = New System.Windows.Forms.ToolBar
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton
Me.SuspendLayout()
'
'ToolBar1
'
Me.ToolBar1.Buttons.AddRange(New
System.Windows.Forms.ToolBarButton() {Me.ToolBarButton1})
Me.ToolBar1.DropDownArrows = True
Me.ToolBar1.Location = New System.Drawing.Point(0, 0)
Me.ToolBar1.Name = "ToolBar1"
Me.ToolBar1.ShowToolTips = True
Me.ToolBar1.Size = New System.Drawing.Size(292, 28)
Me.ToolBar1.TabIndex = 0
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(48, 136)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(206, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(60, 202)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(184, 40)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Save"
'
'ToolBarButton1
'
Me.ToolBarButton1.ToolTipText = "Save"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.ToolBar1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text <> "" Then
MsgBox("TextBox1.Text = " & TextBox1.Text)
End If
End Sub


End Class
 
I

IbrahimMalluf

Hello Tim

Despite Microsoft's attempts to improve control validation through the use
of control focus or validation events, it still is buggy and unpredictable
under certain circumstances. A better approach to form validation (IMHO) is
to use a state-machine approach that is driven independantly from control
events. I am assuming that you want to allow CRUD operations based on the
validity of the form's displayed data. By using a state-machine approach
you can control user options based on data validity regardless of where they
click and in what sequence they click.

Ibrahim
 
G

Gary Chang [MSFT]

Hi Tim,

Thanks for you posting in this group!
How do I validate a control that still has the focus to stop my save
validation event?

As for TextBox control, I think you could put your validation code into
the "Key_Press" event(instead of validate event) to trap the "Return" key
press, it is usual to do a validation check while user press the Return key
in the TextBox, and you also can stop your save validation via setting a
form-scope flag variant(then evaluate it to determine whether or not to
skip the validation routine while the save event is being invoked, and
reset the flag variant before the event function returns).


Best Regards!

Gary Chang
Microsoft Online Partner Support
Get Secure! – www.microsoft.com/security
This posting is provided "AS IS" with no warranties,and confers no rights.
--------------------
 

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