Apply code to multiple textboxes

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
This is some of jmcilhinney's work that I found elsewhere. I would like to apply this to several text boxes on a form.

This link talks about that process but uses slightly different code that I have been advised to avoid. Force only number entering in textboxes-VBForums

Ok.. the code example below specifically refers to textbox name. So my question is :

How can this code be modified so that it could be used in the method of applying the code to multiple textboxes as spoken about in the posted link above.
Of course I could do it manually on each textbox but I would like to avoid that and end up with code that I can then use throughout my app as the need arises. I have modified it slightly already to fit my use..but only the textbox name and length of accepted values.

VB.NET:
Private Sub TextBox16_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox16.KeyPress
        ' Prevent non numeric characters from being entered in textbox
        Dim keyChar = e.KeyChar


        If Char.IsControl(keyChar) Then
            'Allow all control characters.
        ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
            Dim text = Me.TextBox16.Text
            Dim selectionStart = Me.TextBox16.SelectionStart
            Dim selectionLength = Me.TextBox16.SelectionLength


            text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)


            If Integer.TryParse(text, New Integer) AndAlso text.Length > 4 Then
                'Reject an integer that is longer than 16 digits.
                e.Handled = True
            ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
                'Reject a real number with two many decimal places.
                e.Handled = True
            End If
        Else
            'Reject all other characters.
            e.Handled = True
        End If
    End Sub
 
One more critical question regarding formulas.. I think they will be fine but hey.... can't hurt to ask first.

The text boxes are referenced by name in formulas. Will the type conversion cause issues there?
The fields still exist and each one is still a TextBox, courtesy of inheritance, so any existing code that makes use of them will continue to work unchanged.
Actually 2 questions... Looks like I can do a find/replace.... right?
Certainly, although obviously don't use Replace All if you have some other TextBoxes that you don't want to change to numbers-only.

By the way, you should never have something named TextBox14 in an application. You should be changing all the default names to something meaningful that describes the purpose of each control. I sometimes stick with the default names in a test or demo application but, even then, I think I'd be changing the names if I had 14 TextBoxes.
 
The fields still exist and each one is still a TextBox, courtesy of inheritance, so any existing code that makes use of them will continue to work unchanged.

Certainly, although obviously don't use Replace All if you have some other TextBoxes that you don't want to change to numbers-only.

By the way, you should never have something named TextBox14 in an application. You should be changing all the default names to something meaningful that describes the purpose of each control. I sometimes stick with the default names in a test or demo application but, even then, I think I'd be changing the names if I had 14 TextBoxes.


Big, Big Thanks for this! Worked like a charm. I was able to delete other error handling code as a nice side benefit as well.

I do have a question regarding the new simplenumberbox I added. When I email the project to the guy that is working on the physical device that goes with the application, will he need to download and install the new simplenumberbox also?

In other words, does this new tool become a part of VB2010 or is it only contained in this particular project?

If the latter is true, can I make it part of my copy of VB2010 express. Naturally I would like this available all the time in any project I might wander into...

PS...what other nifty tools do you have out there for downloading ? :eek:range:
 
I do have a question regarding the new simplenumberbox I added. When I email the project to the guy that is working on the physical device that goes with the application, will he need to download and install the new simplenumberbox also?

In other words, does this new tool become a part of VB2010 or is it only contained in this particular project?

If the latter is true, can I make it part of my copy of VB2010 express. Naturally I would like this available all the time in any project I might wander into...

If you've added the class to your project then it is part of that project just like any other class in that same project, e.g. your main form. If you move the project then the class goes with it, but nothing outside that project knows about it.

If you get or create any useful types, be they controls or not, then you can put them into a project that compiles to a DLL. You can then use the type(s) defined in that DLL in any project you like. You can add a reference to your project that points to the DLL and then all the public types it contains become accessible. That's exactly how you're able to access types like TextBox and Button, i.e. the System.Windows.Forms.dll assembly is referenced by your project.

You can also add controls and components defined in DLLs to the Toolbox. You can right-click the Toolbox to add new items to a section. If you then drag a SimpleNumberBox from the Toolbox onto a form, VS will generate the required reference automatically. You then have to distribute the DLL containing the SimpleNumberBox along with your EXE.
 

Latest posts

Back
Top