472,351 Members | 1,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

Question about validating event

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
2 2010
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he...
6
by: Michael Rodriguez | last post by:
If you're using a standard 3-tiered architecture, i.e. Data Layer-> Business Layer->Presentation Layer, where is the recommended place to put data...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem....
5
by: Steve | last post by:
I have a datagrid in a WinForm. When the user edits an entry in the datagrid, after he leaves that field, I would like to do some cheking. What...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). ...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button...
6
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.