[Year 12 SofDev] help

Michael Wooldridge mwo at mornsc.vic.edu.au
Mon May 7 10:04:38 EST 2012


Hi guys,

 

I need help. Trying to write validation code that limits a text box entry to
numerical and one decimal place only (and only two decimal places after the
point, as in cash). The following code works fine except that it allows me
to enter letters. The problem seems to be this line: If Not
Char.IsDigit(e.KeyChar) Then 'if not a digit

To me, initially, I would have thought it should read: If Not
e.KeyChar.IsDigit(e.KeyChar) Then 'if not a digit but VB won't let me do
that and keeps telling me to change it back to "Char" 

 

 

I can't figure out why! Strangely, if a number is entered in the text box
first, then I try to type over it with letters, it works perfectly. 

 

If anyone can solve this for me or has a better way of doing it your
suggestions would be much appreciated.

 

 

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

 

        If Not Char.IsDigit(e.KeyChar) Then 'if not a digit

            If Not Asc(e.KeyChar) = 8 Then 'and not a backspace

                If Not TextBox2.Text.LastIndexOf(Chr(46)) = -1 Then 'and a
decimal has already been added, dont allow

                    e.Handled = True

                    MsgBox("numbers only")

                End If

            End If

        Else 'if a digit compare index of decimal point (if it exists) and
length of text

            If Not TextBox2.Text.LastIndexOf(Chr(46)) = -1 Then

                If TextBox2.TextLength > TextBox2.Text.LastIndexOf(Chr(46))
+ 2 Then

                    'if length is over 2 spaces from the decimal point, dont
allow 

                    e.Handled = True

                    MsgBox("only two decimal places allowed")

                End If

            End If

 

        End If

 

    End Sub

 

 

Description: signiture

 

"It is the tragedy of the world that no one knows what he doesn't know - and
the less a man knows, the more sure he is that he knows everything" Joyce
Cary.

 

Michael Wooldridge

Information Technology Coordinator

Mornington Secondary College

1051 Nepean Highway, Mornington 3931

(: 03 59700250 6: 03 59700299

 <mailto:mwo at mornsc.vic.edu.au> mwo at mornsc.vic.edu.au

 

Important - This email and any attachments may be confidential. If received
in error, please contact us and delete all copies. Before opening or using
attachments check them for viruses and defects. Regardless of any loss,
damage or consequence, whether caused by the negligence of the sender or
not, resulting directly or indirectly from the use of any attached files our
liability is limited to resupplying any affected attachments. Any
representations or opinions expressed are those of the individual sender,
and not necessarily those of the Department of Education and Early Childhood
Development. There, that just about covers everyone's arse but yours, sorry.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20120507/b52d7119/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 7858 bytes
Desc: not available
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20120507/b52d7119/attachment-0001.png 


More information about the sofdev mailing list