473,693 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CausesValidatio n
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 CausesValidatio n property. What I am after is
something like the following:

Private Sub txtSourceFile_V alidating(...) Handles txtSourceFile.V alidating
'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_lunch meat_[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 2123
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.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.BtnCancel = New System.Windows. Forms.Button
Me.txtSourceFil e = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
'
'BtnCancel
'
Me.BtnCancel.Lo cation = New System.Drawing. Point(176, 112)
Me.BtnCancel.Na me = "BtnCancel"
Me.BtnCancel.Ta bIndex = 0
Me.BtnCancel.Te xt = "Button1"
'
'txtSourceFile
'
Me.txtSourceFil e.Location = New System.Drawing. Point(176, 48)
Me.txtSourceFil e.Name = "txtSourceF ile"
Me.txtSourceFil e.TabIndex = 1
Me.txtSourceFil e.Text = "TextBox1"
'
'Form8
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(292, 273)
Me.Controls.Add (Me.txtSourceFi le)
Me.Controls.Add (Me.BtnCancel)
Me.Name = "Form8"
Me.Text = "Form8"
Me.ResumeLayout (False)

End Sub

#End Region

Private txtSourceFileNe edValidating As Boolean = False
Private Sub Form8_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
AddHandler Application.Idl e, AddressOf Application_Idl e
End Sub
Private Sub Application_Idl e(ByVal sender As System.Object, ByVal e As
System.EventArg s)
Validating_txtS ourceFile()
End Sub
Private Sub Validating_txtS ourceFile()
If txtSourceFileNe edValidating Then
txtSourceFileNe edValidating = False
'Validating
'Call txtSourceFile.F ocus() if txtSourceFile.t ext doesn't meet
your requirement

End If
End Sub

Private Sub txtSourceFile_V alidating(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles txtSourceFile.V alidating
txtSourceFileNe edValidating = True
End Sub

Private Sub BtnCancel_Click (ByVal sender As Object, ByVal e As
System.EventArg s) Handles BtnCancel.Click
txtSourceFileNe edValidating = False
End Sub
End Class

Nov 21 '05 #2
Chris,

Here is an example:

Private Sub Control_Validat ing(ByVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) _

Handles NameTextBox.Val idating, PayRateTextBox. Validating

Dim controlValidati ng As Control = CType(sender, Control)

MessageBox.Show (controlValidat ing.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_sbcg lobal[dot]]net"> wrote in
message news:bp******** *************** *****@40tude.ne t...
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 CausesValidatio n
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 CausesValidatio n property. What I am after is
something like the following:

Private Sub txtSourceFile_V alidating(...) Handles txtSourceFile.V alidating
'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_lunch meat_[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
5792
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 to leave the textbox without entering the right information. Is there a way to determine which other control was clicked in the validating event of the textbox? Thanks
6
1365
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 as "Please enter a name." It seems like the best place to put them, from the programmer's point of view, is in the business layer. That way the same validations work for either Winforms or Webforms. Also, if I redo my interface later, I don't...
0
1550
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 control which had it's causes validation property set to true however if I tabbed on to this control the event wouldn't fire. So after playing around with my code I figured out how to get it to work. I am not sure what the reason behind it is but it...
5
3338
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 make sure that the value he modified does not create a dup value in my DB. Thank you for your help, Steve
0
2462
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 which are not behaving as I would expect. Specifically, if there is a command button external to the usercontrol which is activated by a shortcut key (eg Alt-B), the command button Click event handler code 'executes' even though the textbox set...
21
9163
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 the cancel button, the textbox.validating is being called. I don't want it to be since they are exiting the screen the validation doesn't have to be done. How can I do that.
6
4749
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 it's .Select() method. This is clunky though because you can see a flash in each text box as it's being validated. Here's my code Private Sub Save() For each c as control in Me.Controls If c.CanSelect() then c.Select()
16
5502
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 validating event. Am I doing something wrong here? Thanks Al Imports system
3
2793
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
8549
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9100
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8815
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6475
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5824
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2989
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1967
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.