[Year 12 SofDev] help

Poultney, Gordon A Poultney.Gordon.A at edumail.vic.gov.au
Mon May 7 11:39:29 EST 2012


hi Michael,

this is how i'd do the dec. place bit

        Dim i As Integer
        Dim length as integer = Len(TextBox1.Text)

        ' validate dec place
        For i = 1 To length
            If Mid(TextBox1.Text, i, 1) = "." Then
                If length - i > 2 Then
                    MsgBox("too many places")
                    Exit Sub
                End If
            End If
        Next

cheers
             Gordon


-----------
Gordon Poultney
Horsham College
________________________________
From: sofdev-bounces at edulists.com.au [sofdev-bounces at edulists.com.au] on behalf of Michael Wooldridge [mwo at mornsc.vic.edu.au]
Sent: Monday, 7 May 2012 10:04 AM
To: Year 12 Software Development Teachers' Mailing List
Subject: [Year 12 SofDev] help

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
mwo at mornsc.vic.edu.au<mailto: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.


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20120507/423b36b1/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 7858 bytes
Desc: image001.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20120507/423b36b1/image001-0001.png 


More information about the sofdev mailing list