checking multiple textboxes for integers only, and if user copys and pastes

Joseph Hedbon 161 Reputation points
2022-05-14T00:01:17.777+00:00

Ok its been a while since i've done coding. So i thought would give it a shot again not doing too bad but having an issue with checking multiple textboxes to make sure only integers are present even when the user copies and pastes into the box. would like it to shift focus to the textboxes that don't have integers but not a necessity. Also i'm trying to run the check before i "call" the rest of my subs or functions. This is a windows form project using VB. currenty i have this as my button click event.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Call current_year_values()
    Call retention_months()
    Call Individual_Retention_Months()
    Call future_retention()


End Sub

i was able to get this however the user can still copy and paste what ever they want into the text boxes.

Private Sub textboxgrosssubmit_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textboxgrosssubmit.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If

End Sub

I have been researching the "try.parse" and found that seems the way to go, however i'm confused on how to set it up.

Thank you so much.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-05-14T05:38:08.75+00:00

    I think that you can remove the textboxgrosssubmit_KeyPress sub, and verify the data in Button1_Click:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim g as Integer
       If Not Integer.TryParse(textboxgrosssubmit.Text, g) Then
          textboxgrosssubmit.Focus
          textboxgrosssubmit.SelectAll
          MsgBox("Bad data")
          Return
       End If
    
      Call current_year_values()
      Call retention_months()
      Call Individual_Retention_Months()
      Call future_retention()
    End Sub
    

    You can also validate other textboxes in the same manner and maybe create some helper functions with repetitive code.


1 additional answer

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-05-14T21:41:36.837+00:00

    Hi

    Here is a stand alone example that may help. This example has a GroupBox1 with lots of different Controls in it. The code checks for valid integer entry and clears the TextBox if not (other actions could be done too). This is just a simple example to show the idea and has no error checking.

    ' Form1 with GroupBox1 contining
    ' several types of Control in prticular
    ' many TextBoxes
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' assign all TextBoxes in GroupBox1 to
            ' the same TextChanged Handler
            For Each c As Control In GroupBox1.Controls
                If c.GetType = GetType(TextBox) Then
                    AddHandler DirectCast(c, TextBox).TextChanged, AddressOf TextBox_TextChanged
                End If
            Next
        End Sub
    
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim tb As TextBox = DirectCast(sender, TextBox)
            Dim v As Integer = 0
            If Integer.TryParse(tb.Text, v) Then
                ' valid integer so far
            Else
                ' if NOT a valid Integer the clear TextBox
                tb.Text = Nothing
            End If
        End Sub
        ' another way would be to add all the
        ' TextBox names to the Handles clause but
        ' if lots of TextBoxes it becomes unwieldy
    
    End Class
    
    0 comments No comments