Click here to Skip to main content
13,410,317 members (36,375 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have a array of textboxes in a form. the array has 10 textboxes in it and i want to validate each textbox on key press it will be very lengthy if i do that so i want to use the krypress through array of textbox i have tries this code can u tell me hoe i should use it with array of textboxes

Dim TxtFrom() As TextBox = {txtfr1, txtfr2, txtfr3, txtfr4, txtfr5, txtfr6, txtfr7, txtfr8, txtfr9}

        Dim TxtTo() As TextBox = {txtTo1, txtTo2, txtTo3, txtTo4, txtTo5, txtTo6, txtTo7, txtTo8, txtTo9}
  Private Sub txtfr1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtfr1.KeyPress
        e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")
    End Sub
Posted 25-Dec-12 5:18am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Make a loop in the array of text boxes; in this loop, add an event handler to every one; in the event handler, use the same method to deal with the sender, which will be a reference to a particular text box.

If this is just validation, is some cases you won't even need to know a reference to each of the text boxes is passed to the event handling method. However, usually this is needed. The only information you can use is the reference to the sender, passed as a first parameter of the event handler.

First, you can always type-case this reference to TextBox, because, in this scenario, this is always the sender. So, you get a reference to an instance of the TextBox. To pass additional information, you can use the property TextBox.Tag (inherited from Control.Tag). In your case, it would be the best to assign an index in your array to this Tag. In the event handler, you type-cast Tag to int and this way know the index of the array element being handled.


I almost forgot, VB.NET does not have '+=' operator for adding a handler to an invocation list of an event instance. You need to use AddHandler. This is shown in a code sample here, see the answer #0:[^].

Pratik65 26-Dec-12 1:44am
its too compliacted i didnt understood the example can u explain me in a simple way please how to use keypress in textbox array
If you don't understand it at all, perhaps you are not ready for UI development yet. It hardly can be simpler than that (and I don't have VB.NET compiler to develop a sample and not going to install it, sorry), and all this is really very simple. Did you understand the code sample I referenced?
However, if you tell me which part you don't understand, I'll gladly clarify it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180221.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100