question

JosephHedbon-9458 avatar image
0 Votes"
JosephHedbon-9458 asked LesHay-2099 answered

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

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.

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered JosephHedbon-9458 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

this is what i was trying to do and thank you. do you know of a way to do all text boxes since i have about 23 text boxes using the control method? if not i can always do individual text boxes.

thanks again

0 Votes 0 ·
LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.