By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

AcceptButton Property question / problem

P: n/a
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.CenterScree n
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

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

When you set e.cancel to true in the validating event it cancels the
ok buttons click event and sets the focus back to the textbox.
http://msdn.microsoft.com/library/de...atingtopic.asp

Ken
-----------------
"lostdreamz" <yb***********@ubgznvy.pbz> wrote in message
news:h9********************************@4ax.com...
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

Nov 20 '05 #2

P: n/a
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

On Tue, 13 Jan 2004 06:31:57 -0500, "Ken Tucker [MVP]"
<vb***@bellsouth.net> wrote:
Hi,

When you set e.cancel to true in the validating event it cancels the
ok buttons click event and sets the focus back to the textbox.
http://msdn.microsoft.com/library/de...atingtopic.asp

Ken
-----------------


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?

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.