Textbox Events

G

Guest

I have a simple form with one button and one text box. In the Form, I create
an array list to track the events by adding a descriptive string item to the
arraylist in each event. I first Click on the Button then Click on the
TextBox and enter an "a" then click on the Button again. The following is
what I get for the event tracking Arraylist:

but - GotFocus
but - Clicked
but - LostFocus
txt - Changed
but - GotFocus
but - Clicked

No where do I get the events Textbox.GotFocus or TextBox.LostFocus or
TextBox.Validating or TextBox.Validated firing. Note that the
TextBox.CauseValidation is set to True at Design time.
 
K

Ken Tucker [MVP]

Hi,

The validating event is fired when the control you are moving to has
it causes validation property set to true. I have in the past deleted a
control and readded it. I found the code in the existing event handlers for
the old control did not work. I had to add a new event handler and copy the
code from the old event handler. Maybe this will help.

Ken
---------------
I have a simple form with one button and one text box. In the Form, I
create
an array list to track the events by adding a descriptive string item to the
arraylist in each event. I first Click on the Button then Click on the
TextBox and enter an "a" then click on the Button again. The following is
what I get for the event tracking Arraylist:

but - GotFocus
but - Clicked
but - LostFocus
txt - Changed
but - GotFocus
but - Clicked

No where do I get the events Textbox.GotFocus or TextBox.LostFocus or
TextBox.Validating or TextBox.Validated firing. Note that the
TextBox.CauseValidation is set to True at Design time.
 
C

Cor Ligthert

Ken,

Txt.changed is fired is in Dennis text. However your answer could have been
true.

Cor
 
C

Cor Ligthert

Dennis,

I tried this,
I put a textbox and a button on a form
I pasted in this code
Than I run the code, and typed in "Dennis" in the textbox. Than it showed
"Hello Dennis".
How do you catch those events, probably is there the problem.

\\\
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
MessageBox.Show("Hello " & TextBox1.Text)
End Sub
///

I hope this helps?

Cor
 
G

Guest

Cor, in each event, I add an item to my tracking arraylist like so:

Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
TrackingArray.Add("txt - Validating")
End Sub

I then have an event on the form that when it is clicked, the program breaks
and I print out the TrackingArray. This gives me a history of what was fired
in what sequence. Note that I also tried adding a message box like you did
and got nothing. Did you check if the gotfocus and lost focus events are
firing? Mine don't. Any idea what the problem is? It's a simple form and
it's really annoying that it doesn't work and follow M'soft's documentation
 
G

Guest

Here's my code and it's in a separate project with only the form with one
button and one text box...very simple.

dim ev as arraylist = new arraylist

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ev.Add("but - Click")
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ev.Add("text - gotfocus")
End Sub

Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs)
ev.Add("text - Validating")
MsgBox("Tracking", MsgBoxStyle.OKOnly, "I'm in Validating")
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ev.Add("Text - LostFocus")
End Sub

Private Sub Button_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.GotFocus
ev.Add("but - GotFocus")
End Sub

Private Sub Button_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.LostFocus
ev.Add("but - LostFocus")
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
ev.Add("text - Changed")
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
Dim i As Integer = 0 'Have a Break set on this line then view ev in
the command editor.
End Sub
End Class
 
C

Cor Ligthert

Dennis,

Do you see that in this code is your code is almost what Ken wrote.

However the strange thing is that you have a handler with the
textbox.textbox changed, and that was in your result text.

So probably you deleted the textbox once. Added it again and did than add
afterwards as well the handler for the textchanged.

Are you trying to confuse us

:)

no problem I hope this helps

Cor
 
C

Cor Ligthert

Ken,

See my last message to Dennis.

Your answer was right,

However it was probably a little bit confusing what Dennis did.

Cor
 
G

Guest

I am not trying to confuse anyone. I did not delete the text box at any
time. For the sake of argument, I tried it again exactly as I did it the
first time. I started a new project then added a button1 and text box and the
public variable "eventtracker as an arraylist. This time it seemed to track
all the events ok. However, the textbox1.LostFocus event fired before the
textbox1.Validating event which is contrary to the M'soft's documentation
which says the validating event fires before the lostfocus event. This
eratic behavior of the control events makes if difficult and seems to be
inexcusable on Microsoft's part. Here is the complete project 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 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(184, 146)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(76, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(184, 62)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(84, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 262)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Public eventtracker As ArrayList = New ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles TextBox1.GotFocus
eventtracker.Add("textbox1 - gotfocus")
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles TextBox1.TextChanged
eventtracker.Add("textbox1 - TextChanged")
End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles TextBox1.Leave
eventtracker.Add("textbox1 - Leave")
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles TextBox1.LostFocus
eventtracker.Add("textbox1 - Lostfocus")
End Sub


Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As _
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
eventtracker.Add("textbox1 - Validating")
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Button1.Click
eventtracker.Add("button1 - Click")
End Sub


Private Sub Button1_GotFocus(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Button1.GotFocus
eventtracker.Add("button1 - gotfocus")
End Sub

Private Sub Button1_LostFocus(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Button1.LostFocus
eventtracker.Add("button1 - lostfocus")
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.Click
Dim s As String
Dim i As Integer
While i < eventtracker.Count
s = s & eventtracker(i).ToString & vbLf
i = i + 1
End While
MsgBox(s, MsgBoxStyle.OKOnly, "Event Track...")

End Sub
End Class
 
G

Guest

I see what happened now. The handles xxxx.xx somehow got removed from the
sub statement. I did not delete the text box and have no idea how they got
removed. Still, the TextBox lostfocus event fires before the Validating
event.
 
C

Cor Ligthert

Dennis,

It is not easy to find, however AFAIK those events fire in two different
sequences.

Maybe you can look for it yourself on MSDN.

I thought that Peter Huang, Jeffrey Tan or a collegue of those once has sent
a message about this to this newsgroup.

Cor
 

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

Similar Threads

Click Event 4
Textbox question 7
Click & Focus 10
Event order problem 3
Validating textbox w/ Cancel button 11
Form level focus monitor 4
select text in textbox 8
Radiobutton CheckChanged 2

Top