Capture events for a control array

J

John Wright

I have 80 textboxes on my form that I want to capture the focus event and the
validating event. When someone enters any textbox, I want to capture the
current value of textbox in a variable then on the validate event check the
new value to see if there is a change, and if changed, set the background of
the control to a different color to indicate a change. I can do this for all
80 textboxes, but it would be nice to have these events watch all textboxes
(similiar to the old control array in VB). Anyone have any suggestions.
 
T

Tom Shelton

I have 80 textboxes on my form that I want to capture the focus event and the
validating event. When someone enters any textbox, I want to capture the
current value of textbox in a variable then on the validate event check the
new value to see if there is a change, and if changed, set the background of
the control to a different color to indicate a change. I can do this for all
80 textboxes, but it would be nice to have these events watch all textboxes
(similiar to the old control array in VB). Anyone have any suggestions.

You can attache the same event to multiple textboxes..

Just select the ones you want, go to the properties window, got to the events
tab and then add the event procedure you want to handle....

Option Explicit On
Option Strict On

Public Class Form1

Private Sub TextBoxes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
Dim tb As TextBox = TryCast(sender, TextBox)
If tb IsNot Nothing Then
MessageBox.Show(tb.Name)
End If
End Sub
End Class

You can do it manually, it's just adding more controls to the handles clause
:) The sender parameter will be the actual textbox that raised the event.
So, in the above example I simply cast it to a textbox.
 
J

John Wright

Thanks this helped a lot

Tom Shelton said:
You can attache the same event to multiple textboxes..

Just select the ones you want, go to the properties window, got to the events
tab and then add the event procedure you want to handle....

Option Explicit On
Option Strict On

Public Class Form1

Private Sub TextBoxes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
Dim tb As TextBox = TryCast(sender, TextBox)
If tb IsNot Nothing Then
MessageBox.Show(tb.Name)
End If
End Sub
End Class

You can do it manually, it's just adding more controls to the handles clause
:) The sender parameter will be the actual textbox that raised the event.
So, in the above example I simply cast it to a textbox.
 
J

Johnny Jörgensen

You can make a custom control that monitors the TextChange event of all
textboxes. That way you don't have to worry if and when you put a new
textbox on the form.

I did a control a while ago that does something completely different but
still to solve a similar problem.

You can check it out with source code here:
http://www.codeproject.com/KB/miscctrl/DirtyButton.aspx

Best regards,
Johnny J.
 

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