Running isnumeric on array textbox (Help needed)

hi, this is what i wrote :

Private sub text1_change(index as integer)

Dim A as boolean, b as boolean

for index = 0 to 2
if a = true then msgbox "ok", vbok
if b = true then a = true

if isnumeric(text1(index).text) then
b = false
else : b = true
End if

if text1(index).text = "" then
b = false
End if

Next index

End sub


My form has  3 textboxes in an array. The first box adhere to the above, 
i.e. when i type numbers in,it is okie, however any alphabets will result in 
error msg.

2nd and 3rd text boxes however displays an error msg wheneva i type anything 
in. 

Is there something wrong in what i have done 

thanks~!! Rather urgent


Answer this question

Running isnumeric on array textbox (Help needed)

  • Jagan P

    The only issue with that nobugz can be raising the validating event... i see a lot of posts where people get confused as to when it is and isn't raised

    A lot of times they want to stop the editing directly; e.g. before the textbox looses focus.

    The method I posted is not defeated by pasting, and the proper formatting could be applied by expanding the code a little and using the String methods.

    But I would agree that if you can wait for the validating event to be raised, it is the best place for validation code.



  • Bader Cheema

    Hi nobbie,

    The first thing that comes to mind is that you are testing the 'a' & 'b' booleans at the start of the For...Next loop. By default booleans are set to False. I would think this test would be better suited at the end of the coding; unless you are wanting to test the previous For...Next loop results prior to starting the current loop.

    The second thing is that I don't see any portion of the coding that changes the value of boolean variable 'a'; this is not causing the coding to fail; however, it is interesting that you would test for condition 'a' being True and no area of this coding could possibly change this variable to meet the condition. I would suggest eliminating the test as it will always return False; or put something in there that would change the value of 'a' should the requirements of your condition be met. The only other thought would come to my mind as to why you would test for 'a' to be true is that another procedure might change the value of 'a' to True; if this is the case then you don't want to declare the variable locally (i.e. don't 'Dim a as boolean' inside this Private procedure); you would want to change 'a' to be a Public or Shared variable. This is just my opinion and should be taken with a grain of salt.

    The main thing that I see going on here is that you are not resetting the values of the 'a' & 'b' booleans. Hence, if 'b' is set to True during your 'isnumeric' call then after you complete the call you will want to reset the value before the next loop. Otherwise, as stated above, you are testing results of the previous For...Next loop results. I would add just before your coding says 'next index':

    a = false
    b = false

    This will reset the variables to the default values and thus you would be starting from scratch at the start of your next loop. I would also strongly suggest re-determining where you test for variable 'a' & 'b'. Think of this like a recipe for cake; you can't taste the results of your cake until you have mixed everything together first. If you tried to taste the cake before mixing the ingredients you would simply just taste what each individual ingredient tastes like and not what the mixture of them together would taste like.

    I hope the above helps.

    Thank you,

    James



  • OliWarner

    I prefer using the Validating event. It won't be defeated when the user pastes the text from the clipboard. And you can properly format the entered text, getting rid of silly typo's like a leading zero or inconsistent number of digits in the fraction. For example:

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If TextBox1.Text = "" Then Return
    If Not IsNumeric(TextBox1.Text) Then
    MsgBox("Please enter a valid number", MsgBoxStyle.Exclamation, "Nobugz waz here")
    TextBox1.SelectionStart = 0
    TextBox1.SelectionLength = TextBox1.Text.Length
    e.Cancel = True
    Else
    TextBox1.Text = String.Format("{0:N2}", CDbl(TextBox1.Text))
    End If
    End Sub



  • DevilDog74

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _

    Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress

    If Not IsNumeric(e.KeyChar) Then

    e.Handled = True

    End If

    End Sub

    That will only allow numeric characters to be entered in the three textboxes



  • FlyinBrian

    The only problem with this simple method is that it only allows integers. For full decimal values, you need a slightly more complex routine (characters like a period or comma need to be allowed as appropriate for decimal values).

    Something like the following will allow decimal values but not letters or other punctuation:

    Dim prevval As String

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim tb As TextBox = CType(sender, TextBox)
    prevval = tb.Text
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim tb As TextBox = CType(sender, TextBox)
    If Not Decimal.TryParse(tb.Text, Nothing) Then
    tb.Text = prevval
    End If
    End Sub

    Which route you take just depends on what values you're looking for; integers or decimals



  • Running isnumeric on array textbox (Help needed)