vb.net code question
-
I am new to the vb.net thing and am trying to self teach myself so any help is appreciated. I need to be able to make sure that a textbox entry is only numbers and that I can backspace it to remove a bad entry. I think its probably something like a bunch of ASCII characters to reference and valadate the entry but I am not sure. Sub Text_KeyPress(KeyAscii As Integer) If uiEnterInterNumTextBox.Text = KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 and uiEnterInterNumTextBox.Text = KeyAscii End Sub
-
I am new to the vb.net thing and am trying to self teach myself so any help is appreciated. I need to be able to make sure that a textbox entry is only numbers and that I can backspace it to remove a bad entry. I think its probably something like a bunch of ASCII characters to reference and valadate the entry but I am not sure. Sub Text_KeyPress(KeyAscii As Integer) If uiEnterInterNumTextBox.Text = KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 and uiEnterInterNumTextBox.Text = KeyAscii End Sub
heres how I would do it. remember, in vb, try not to do it the hard way. most of the time you'll find an easy one. BTW somthing seems wrong with your keypress event handler, I'm used to Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress now if your working in vb 6 or VBA this code may not work. by the way if this is homework, at least paraphrase... geta little edu for your money. Sub Text_KeyPress(KeyAscii As Integer) If isAllNumbers(uiEnterInterNumTextBox.Text)Then KeyAscii = 0 uiEnterInterNumTextBox.Text = KeyAscii End If End Sub Private Function isAllNumbers(ByVal teststring As String) As Boolean Dim isgood As Boolean = True For Each c As Char In teststring If Not Char.IsDigit(c) and not(c = ".") and not( c = ",") Then isgood = False Exit For End If Next Return isgood End Function hey...slang is the vernacular for the vernacular...wow
-
I am new to the vb.net thing and am trying to self teach myself so any help is appreciated. I need to be able to make sure that a textbox entry is only numbers and that I can backspace it to remove a bad entry. I think its probably something like a bunch of ASCII characters to reference and valadate the entry but I am not sure. Sub Text_KeyPress(KeyAscii As Integer) If uiEnterInterNumTextBox.Text = KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 and uiEnterInterNumTextBox.Text = KeyAscii End Sub
Or You can use a MaskedTestbox to do it Or
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If (AscW(e.KeyChar) < 48 Or AscW(e.KeyChar) > 57) Then e.KeyChar = Nothing End If End Sub
Regards,
Chatura Dilan
If at first you don't succeed, try; try again, so that you know what not to do the next time. The answer is out there. -- modified at 23:25 Tuesday 4th April, 2006 'sorry.. when you use
e.KeyChar = Chr(Keys.Back),
other keys always do backspace's job. I corrected it. It must bee.KeyChar = Nothing
-
Or You can use a MaskedTestbox to do it Or
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If (AscW(e.KeyChar) < 48 Or AscW(e.KeyChar) > 57) Then e.KeyChar = Nothing End If End Sub
Regards,
Chatura Dilan
If at first you don't succeed, try; try again, so that you know what not to do the next time. The answer is out there. -- modified at 23:25 Tuesday 4th April, 2006 'sorry.. when you use
e.KeyChar = Chr(Keys.Back),
other keys always do backspace's job. I corrected it. It must bee.KeyChar = Nothing