Click here to Skip to main content
12,507,674 members (53,883 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB
hei guys
this is the sample code of mine
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not IsNumeric(TextBox1.Text) Then
hehe:
            Dim abc As DialogResult = MessageBox.Show("Please insert numeric only!", "Error!!", MessageBoxButtons.OKCancel)
            If abc = DialogResult.OK Then
                TextBox1.Clear()
            Else
                GoTo hehe
            End If
        End If
    End Sub

after i press on the ok button..
my text have been clear..
but the problem is the message box still pop out
what show i do to prevent the messagebox pop out again when the ok button is clicked
Posted 17-Feb-13 23:49pm
Updated 18-Feb-13 0:02am
v4
Comments
Abhinav S 18-Feb-13 5:56am
   
Something seems to be missing. Appears as if you are calling the same method again and again.
Post complete / more code here.
arnoldxx 18-Feb-13 6:02am
   
done..
this is my complete code~
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

The message box appearing, because, once you clear the textbox, again its a textchange event, so, it is becoming an infinite function.
so, try below code.
If TextBox1.Text.Trim <> "" Then
    If Not IsNumeric(TextBox1.Text) Then
            Dim abc As DialogResult = MessageBox.Show("Please insert numeric only!", "Error!!", MessageBoxButtons.OKCancel)
            If abc = DialogResult.OK Then
                TextBox1.Clear()
            End If
    End If
End If

hope it helps
  Permalink  
v2
Comments
arnoldxx 18-Feb-13 9:48am
   
thx.. btw..
what is the function of <>
Karthik Harve 18-Feb-13 23:09pm
   
welcome.
arnoldxx 18-Feb-13 9:54am
   
sorry i mean trim *
Karthik Harve 18-Feb-13 23:08pm
   
it will trim the textbox text if any spaces on both the ends.
arnoldxx 19-Feb-13 1:34am
   
ic...
thx lot for helping me.. :)
Karthik Harve 19-Feb-13 1:40am
   
welcome .!!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I suggest you:
1) to use TextBox_Leave[^] event, not TextBox_TextChange[^] event.
2) do not remove text from TextBox, but select it and bring focus back into TextBox,
3) use RegEx[^] to check if TextBox "value" is numeric value.
  Permalink  

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 | Mobile
Web02 | 2.8.160927.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2016
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