By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,972 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Question about validating event

P: n/a
I have a form with a textbox and numerous panels, buttons and other
controls. I have handled the textbox Validating and Validated events. The
textbox will hold a filename. In the validating event, I check that the
string in the textbox is a file that exists or whether or not the string is
blank and display a message box in either case. I also call e.Cancel so
that the value will be corrected.

However, certain buttons on the form (such as a cancel button) I don't want
causing the validation code to run, so I set their CausesValidation
property to False. This has met with limited success. I still get the
validating code executed in situations I don't want it.

Is there a way to find out which control caused the validation to occur? I
only want this for debugging purposes so I can see what controls I have
missed when setting their CausesValidation property. What I am after is
something like the following:

Private Sub txtSourceFile_Validating(...) Handles txtSourceFile.Validating
'Some code here to determine which control caused the
'validating event to occur.
End Sub

Can you think of any other way to get the information I need?

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
A solution:

Public Class Form8
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 BtnCancel As System.Windows.Forms.Button
Friend WithEvents txtSourceFile As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.BtnCancel = New System.Windows.Forms.Button
Me.txtSourceFile = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'BtnCancel
'
Me.BtnCancel.Location = New System.Drawing.Point(176, 112)
Me.BtnCancel.Name = "BtnCancel"
Me.BtnCancel.TabIndex = 0
Me.BtnCancel.Text = "Button1"
'
'txtSourceFile
'
Me.txtSourceFile.Location = New System.Drawing.Point(176, 48)
Me.txtSourceFile.Name = "txtSourceFile"
Me.txtSourceFile.TabIndex = 1
Me.txtSourceFile.Text = "TextBox1"
'
'Form8
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.txtSourceFile)
Me.Controls.Add(Me.BtnCancel)
Me.Name = "Form8"
Me.Text = "Form8"
Me.ResumeLayout(False)

End Sub

#End Region

Private txtSourceFileNeedValidating As Boolean = False
Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler Application.Idle, AddressOf Application_Idle
End Sub
Private Sub Application_Idle(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Validating_txtSourceFile()
End Sub
Private Sub Validating_txtSourceFile()
If txtSourceFileNeedValidating Then
txtSourceFileNeedValidating = False
'Validating
'Call txtSourceFile.Focus() if txtSourceFile.text doesn't meet
your requirement

End If
End Sub

Private Sub txtSourceFile_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles txtSourceFile.Validating
txtSourceFileNeedValidating = True
End Sub

Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles BtnCancel.Click
txtSourceFileNeedValidating = False
End Sub
End Class

Nov 21 '05 #2

P: n/a
Chris,

Here is an example:

Private Sub Control_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) _

Handles NameTextBox.Validating, PayRateTextBox.Validating

Dim controlValidating As Control = CType(sender, Control)

MessageBox.Show(controlValidating.Name & " is validating.")

End Sub

You can add more controls to the Handles clause i.e. all the controls you
want to monitor.
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:bp****************************@40tude.net...
I have a form with a textbox and numerous panels, buttons and other
controls. I have handled the textbox Validating and Validated events.
The
textbox will hold a filename. In the validating event, I check that the
string in the textbox is a file that exists or whether or not the string
is
blank and display a message box in either case. I also call e.Cancel so
that the value will be corrected.

However, certain buttons on the form (such as a cancel button) I don't
want
causing the validation code to run, so I set their CausesValidation
property to False. This has met with limited success. I still get the
validating code executed in situations I don't want it.

Is there a way to find out which control caused the validation to occur?
I
only want this for debugging purposes so I can see what controls I have
missed when setting their CausesValidation property. What I am after is
something like the following:

Private Sub txtSourceFile_Validating(...) Handles txtSourceFile.Validating
'Some code here to determine which control caused the
'validating event to occur.
End Sub

Can you think of any other way to get the information I need?

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.