Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Button Click not firing after Validation event
 

Button Click not firing after Validation event

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.  
 
MigrationUser 1  Wednesday, July 02, 2003 9:19 AM
Strange... Works fine for me here.  Can you post a little bit of code that reproduces the problem?
MigrationUser 1  Wednesday, July 02, 2003 10:24 AM
Here's everything in the project :

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
MigrationUser 1  Wednesday, July 02, 2003 11:13 AM
Weird... That doesn't work for me in that scenario...  I wonder if that's a known bug?  Perhaps somebody more in the know can chime in with an explanation.  This particular scenario works for me when I display the form using ShowDialog and return a DialogResult in my button click handler.
MigrationUser 1  Wednesday, July 02, 2003 11:21 AM
Jacob

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
MigrationUser 1  Wednesday, July 02, 2003 12:02 PM
Okay, the bug appears to be the MessageBox in the validating event.  If you change it to Debug.Writeline("Validating"), it should work for you.
MigrationUser 1  Wednesday, July 02, 2003 3:09 PM
Thanks - stepping through in debug also seems to cause the problem.

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?
MigrationUser 1  Wednesday, July 02, 2003 6:31 PM
Okay, I just whipped up a little test that seems to work (this is a single form with a button and a textbox.  The button has CausesValidation=True):

    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?
MigrationUser 1  Wednesday, July 02, 2003 6:54 PM
Thanks again Jacob.

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
MigrationUser 1  Thursday, July 03, 2003 5:04 AM
Microsoft Tech. Support (incident no. SRZ030703000446) have provided this workaround which works well:

    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.
MigrationUser 1  Friday, July 04, 2003 9:13 AM

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?

JonneeZ  Thursday, October 19, 2006 4:08 PM

You can use google to search for other answers

Custom Search

More Threads

• Create a designable base control? / Code generation for subclassed controls
• pattern: set, get property
• Customize appearance of DesignSurface.View
• beta 2 - Negative Min value causes error
• Showing Current Date and Time without using Timer in VB.Net 2005?
• this.Refresh(); problem in C#
• How to built tab like Ribbon
• Replacement of ImageList Collection Editor
• EventsTab and PropertyGrid
• prevent from resizing in desgin-time