473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2096
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 clicks on another specific control he is allowed...
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 validations? Specifically, data validations such...
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. The event would fire when clicking on another...
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 event fires when the user does that? I need to...
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). I have controls (textboxes) within UserControls...
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 is the Cancel button. WHen the user clicks on...
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 this is to cycle through every control and call...
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 doesn't fire if you open a dialog box in button 1's...
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 out of it (TextBox loose focus)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.