I have a textbox and a button my form. My button has CauseValidation turned on. If I click the button, the textbox validation fires. If the validation is successful, the button receives focus, but the Click event is not fired.
I would like the click event to fire if the validation is successful.

Button Click not firing after Validation event
dreyx2000
J. JIMENEZ
Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
Dim c As Control = Me.ActiveControl
Dim t As Type = c.GetType
Dim mi As MethodInfo = t.GetMethod("OnClick", BindingFlags.NonPublic Or BindingFlags.Instance, _
Nothing, New Type() {GetType(EventArgs)}, Nothing)
mi.Invoke(c, New Object() {e})
End Sub
This in addition to the Validating and Click event handlers.
wixy
In effect, you're suggesting I use the Validating event handler on the textbox as the Click event handler for the button. This would be fine if clicking the button was the only way the textbox was validated. If I have other controls on the form, the Validating event could be triggered by other user actions.
I still have the problem that if I want to give my users the choice to continue or cancel as in the following example, I can't.
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If MessageBox.Show("Continue", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
GianfrancoV
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 Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 96)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(152, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Clicked")
End Sub
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
MessageBox.Show("Validating")
End Sub
End Class
paulballard
Can you post the code in your scenario, i.e, where the click event fires after the validating event
This strikes me as a fairly fundamental problem if it is a bug - how does one report it to Microsoft Technical Support
Joe
Philip W
I have the same scenario, but this workaround didn't help me.
I have many controls visible simultaneously on my Windows Form. One of them is an "editor," and when the user tries to leave that control by clicking on another control, I want to prompt the user to save their editing session, with a "Yes/No/Cancel" prompt. If the user clicks "Yes" or "No", then (after saving if they clicked "Yes"), the original click event should be processed. if they click "Cancel," the user should remain in the original "editor" control.
I tried looking at the ActiveControl in the "editor's" Validated event handler. But the value was always the ToolStripContainer of my main form, rather than the specific control that was clicked.
Is there another workaround, or some other way to handle this scenario
nolan.bailey
Private Sub TextboxValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If MessageBox.Show("This will*space
space*Stuff, would you like to continue ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'Break Stuff Here
Debug.WriteLine("Stuff broken")
Else
Debug.WriteLine("Stuff not broken")
e.Cancel = True
End If
End Sub
I think that the button click doesn't fire because it receives the focus, but Validation kicks in and interrupts any subsequent events, including MouseDown. You'll notice that the button doesn't make a visual representation of being depressed.
So, conceivably, you can pull out the code in the Button Click handler and place it in another fucntion. In the Button Click Handler, then, you'll place a call to the function. Below the "Stuff broken" line above, you'll also make a call to that same function. That should get you well on your way...
Make sense
Eugene Osovetsky
meshman
I have a scenario where I need to offer a warning to the user during validation to which they may respond to continue or abort. A messagebox in the validating event handler seemed the ideal solution - what's the recommended workaround
Phillip Misner MSFT