472,958 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Validating Event in a UserControl

I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852 (http://support.microsoft.com/kb/810852), but then I realized that the hotfix mentioned was in .Net v1.1, which I am using.

I took the sample from that article and recreated the situation I see in my application. (Code included below.)

If you run the enclosed application and initially navigate to Tab2 and in the text box type an "A" (no quotes), then click on the plus sign next to the node labeled root, you get a message box as expected. Click OK in that dialog box and in the results textbox you see that a "before select", and "after select" event fired in the tree control and THEN the validating event in the textbox on Tab2 fired (it displays the word "Cancel"). Click the plus sign again and click ok in the dialog box. This time ONLY the validating event fires, none of the tree view events fire. Now wait, it gets stranger!!

Now close the app and run it again. This time, first click on the plus sign next to the root node. Now go to tab 2 and in the text box type an "A". (no quotes). Now click on the same plus sign next to the root node again. This time only the validating event fires. Click on the plus sign next to either child node. Again, only the validating event fires.

Now my question, if a validating event sets e.cancel to true why would ANY subsequent event handlers fire? Actually looking at the results it seems that in the first test the "before select" and "after select" events fired BEFORE the validating event on tab2, and the only the "before expand" event was inhibited by the e.cancel.

I would think that all validating events would get precedence and other events would not fire until the validating events were complete. And that does indeed seem to be the case on subsequent clicks in the first test case and on ALL clicks in the second test case.

This has just about driven me nuts, (yeah I know it's not a drive it's a short walk). I am using a tree as a navigation element and after the user changes data on the tab pages I attempt to update the database. On constraint errors, I don't want the user to be able to navigate away from the tab. But if the tree events are allowed to fire that is EXACTLY what will happen. The user will be allowed to navigate away from the existing tab and pending changes will be lost.

Can anyone shed some light on this?

Gary Shell
________________
code sample:
Public Class Form1

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 UserControl11 As WindowsApplication1.UserControl1

Friend WithEvents TreeView1 As System.Windows.Forms.TreeView

Public WithEvents Results As System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.UserControl11 = New WindowsApplication1.UserControl1

Me.TreeView1 = New System.Windows.Forms.TreeView

Me.Results = New System.Windows.Forms.TextBox

Me.SuspendLayout()

'

'UserControl11

'

Me.UserControl11.Location = New System.Drawing.Point(216, 16)

Me.UserControl11.Name = "UserControl11"

Me.UserControl11.Size = New System.Drawing.Size(320, 240)

Me.UserControl11.TabIndex = 0

'

'TreeView1

'

Me.TreeView1.ImageIndex = -1

Me.TreeView1.Location = New System.Drawing.Point(32, 16)

Me.TreeView1.Name = "TreeView1"

Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("root", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("child 1", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("grandchild1")}), New System.Windows.Forms.TreeNode("child2", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("grandchild 2")})})})

Me.TreeView1.SelectedImageIndex = -1

Me.TreeView1.Size = New System.Drawing.Size(136, 112)

Me.TreeView1.TabIndex = 3

'

'Results

'

Me.Results.Location = New System.Drawing.Point(32, 144)

Me.Results.Multiline = True

Me.Results.Name = "Results"

Me.Results.ScrollBars = System.Windows.Forms.ScrollBars.Vertical

Me.Results.Size = New System.Drawing.Size(136, 104)

Me.Results.TabIndex = 4

Me.Results.Text = ""

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(568, 266)

Me.Controls.Add(Me.Results)

Me.Controls.Add(Me.TreeView1)

Me.Controls.Add(Me.UserControl11)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TreeView1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect

results.Text = results.Text & "before select" & vbCrLf

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

results.Text = results.Text & "after select" & vbCrLf

End Sub

Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

results.Text = results.Text & "before expand" & vbCrLf

End Sub

End Class

-----------------------------------

Public Class UserControl1

Inherits System.Windows.Forms.UserControl

#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

'UserControl1 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 TabControl1 As System.Windows.Forms.TabControl

Friend WithEvents TabPage1 As System.Windows.Forms.TabPage

Friend WithEvents TabPage2 As System.Windows.Forms.TabPage

Friend WithEvents UserControl21 As WindowsApplication1.UserControl2

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TabControl1 = New System.Windows.Forms.TabControl

Me.TabPage1 = New System.Windows.Forms.TabPage

Me.TabPage2 = New System.Windows.Forms.TabPage

Me.UserControl21 = New WindowsApplication1.UserControl2

Me.TabControl1.SuspendLayout()

Me.TabPage2.SuspendLayout()

Me.SuspendLayout()

'

'TabControl1

'

Me.TabControl1.Controls.Add(Me.TabPage1)

Me.TabControl1.Controls.Add(Me.TabPage2)

Me.TabControl1.Location = New System.Drawing.Point(8, 8)

Me.TabControl1.Name = "TabControl1"

Me.TabControl1.SelectedIndex = 0

Me.TabControl1.Size = New System.Drawing.Size(304, 216)

Me.TabControl1.TabIndex = 0

'

'TabPage1

'

Me.TabPage1.Location = New System.Drawing.Point(4, 22)

Me.TabPage1.Name = "TabPage1"

Me.TabPage1.Size = New System.Drawing.Size(296, 190)

Me.TabPage1.TabIndex = 0

Me.TabPage1.Text = "TabPage1"

'

'TabPage2

'

Me.TabPage2.Controls.Add(Me.UserControl21)

Me.TabPage2.Location = New System.Drawing.Point(4, 22)

Me.TabPage2.Name = "TabPage2"

Me.TabPage2.Size = New System.Drawing.Size(296, 190)

Me.TabPage2.TabIndex = 1

Me.TabPage2.Text = "TabPage2"

'

'UserControl21

'

Me.UserControl21.Location = New System.Drawing.Point(24, 24)

Me.UserControl21.Name = "UserControl21"

Me.UserControl21.TabIndex = 0

'

'UserControl1

'

Me.Controls.Add(Me.TabControl1)

Me.Name = "UserControl1"

Me.Size = New System.Drawing.Size(320, 240)

Me.TabControl1.ResumeLayout(False)

Me.TabPage2.ResumeLayout(False)

Me.ResumeLayout(False)

End Sub

#End Region

End Class

------------------------------------

Public Class UserControl2

Inherits System.Windows.Forms.UserControl

#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

'UserControl 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 TextBox1 As System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.SuspendLayout()

'

'TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point(8, 40)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.Size = New System.Drawing.Size(128, 20)

Me.TextBox1.TabIndex = 0

Me.TextBox1.Text = "TextBox1"

'

'UserControl2

'

Me.Controls.Add(Me.TextBox1)

Me.Name = "UserControl2"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub UserControl2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating

If LCase(TextBox1.Text) = "a" Then

MessageBox.Show("Invalid entry. Reenter data again.")

e.Cancel = True

Dim myform As Form1

myform = Me.Parent.Parent.Parent.Parent

'the parent is the tab page, its parent is the tab control,

'its parent is the usercontrol and its parent is the form. Whew!

myform.Results.Text = myform.Results.Text & "Cancel" & vbCrLf

Else

e.Cancel = False

End If

End Sub

End Class

Nov 21 '05 #1
0 2381

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...
2
by: Eric Maia | last post by:
I have a textbox (StartDateTextBox) in a UserControl on my page, that is supposed to have a date entered into it. I have a RequiredFieldValidator that has its ControlToValidate property set to the...
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...
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...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
5
by: seal | last post by:
OK, I am trying to fire an event from a usercontrol that tells the page when a checkbox was clicked. Here is the control and when I try and wire up the page to catch the event, my event is not...
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...
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)
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.