Home All Groups Group Topic Archive Search About

Capture events for a control array

Author
9 Jul 2009 10:22 PM
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.

Author
9 Jul 2009 10:59 PM
Tom Shelton
On 2009-07-09, John Wright <JohnWri***@discussions.microsoft.com> wrote:
> 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.

--
Tom Shelton