AcceptButton Property question / problem

L

lostdreamz

Ok, my situation is like this.

I have a form with a few text boxes each with a 'validating' event, OK
& Cancel buttons, and have the AcceptButton/CancelButton/DialogResult
properties all set.

Situation 1: (works fine)
If the user enters invalid text in a text box, presses Enter in the
text box, the validation displays a msgbox. They now correct the text,
press Enter in the text box, it validates correctly this time and the
OK button click event fires.

Situation 2: (does not work)
If the user enters invalid text in a text box, clicks the OK button,
the validation displays a msgbox. They now correct the text, press
Enter in the text box, it validates correctly this time BUT the OK
button click event does not fire.

Why is situation 2 not working?


lostdreamz

-----------------------------
To recreate the situation 2 described above, create a new solution and
paste the code below over a blank form's code:
-----------------------------

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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32,
16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(104, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.DialogResult =
System.Windows.Forms.DialogResult.OK
Me.Button1.FlatStyle =
System.Windows.Forms.FlatStyle.System
Me.Button1.Location = New System.Drawing.Point(48, 64)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 3
Me.Button1.Text = "ok"
'
'Form1
'
Me.AcceptButton = Me.Button1
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(168, 102)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal
e As System.ComponentModel.CancelEventArgs) Handles
TextBox1.Validating
If TextBox1.Text = "" Then
MsgBox("cannot be empty")
e.Cancel = True
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
End
End Sub

End Class
 
L

lostdreamz

Ken,

Thanks for responding. Perhaps you misread was I was saying in
Situation 2 yesterday.

The only way I can show you what I'm talking about is to tell you the
exact steps to recreate the problem.

1. Use the code for the form I sent you yesterday, or create a form
of your own with a text box and a button use the code below for the
validate event of the text box.

If TextBox1.Text = "" Then
MsgBox("cannot be empty")
e.Cancel = True
End If

2. Now, press the OK button when the textbox is clear and you will
get the message box. Click OK on the message box.

3. Now, type some text in the text box.

4. Now, this time instead of clicking the OK button to try to fire
it's Click event, I want you to press the Enter key while the focus is
still in the text box.

5. Notice, this time the validation was successful, but the Click
event was not fired from the Enter key being pressed in the textbox.

Note: If you do not click on the OK button, but always just press the
Enter key to fire the OK button Click event, the problem I described
above does not occur.

lostdreamz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top