Have you thought about using an input mask?<br><br><div class="gmail_quote">On 7 May 2012 10:04, Michael Wooldridge <span dir="ltr"><<a href="mailto:mwo@mornsc.vic.edu.au" target="_blank">mwo@mornsc.vic.edu.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div link="blue" vlink="purple" lang="EN-AU"><div><p class="MsoNormal">Hi guys,<u></u><u></u></p><p class="MsoNormal">
<u></u> <u></u></p>
<p class="MsoNormal" style="text-autospace:none">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: <span style="font-size:9.5pt;font-family:Consolas;color:blue">If</span><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">Not</span> <span style="color:blue">Char</span>.IsDigit(e.KeyChar) Then <span style="color:green">'if not a digit</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none">To me, initially, I would have thought it should read: <span style="font-size:9.5pt;font-family:Consolas;color:blue">If</span><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">Not</span> e.KeyChar.IsDigit(e.KeyChar) <span style="color:blue">Then</span> <span style="color:green">'if not a digit </span></span>but VB won’t let me do that<span style="font-size:9.5pt;font-family:Consolas"> </span>and keeps telling me<span style="font-size:9.5pt;font-family:Consolas"> to change it back to “Char” <u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">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. <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">If anyone can solve this for me or has a better way of doing it your suggestions would be much appreciated.<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">Private</span> <span style="color:blue">Sub</span> TextBox2_KeyPress(<span style="color:blue">ByVal</span> sender <span style="color:blue">As</span> <span style="color:blue">Object</span>, <span style="color:blue">ByVal</span> e <span style="color:blue">As</span> System.Windows.Forms.<span style="color:#2b91af">KeyPressEventArgs</span>) <span style="color:blue">Handles</span> TextBox2.KeyPress<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">If</span> <span style="color:blue">Not</span> <span style="color:blue">Char</span>.IsDigit(e.KeyChar) <span style="color:blue">Then</span> <span style="color:green">'if not a digit</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">If</span> <span style="color:blue">Not</span> Asc(e.KeyChar) = 8 <span style="color:blue">Then</span> <span style="color:green">'and not a backspace</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">If</span> <span style="color:blue">Not</span> TextBox2.Text.LastIndexOf(Chr(46)) = -1 <span style="color:blue">Then</span> <span style="color:green">'and a decimal has already been added, dont allow</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> e.Handled = <span style="color:blue">True</span><u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas"> MsgBox(<span style="color:#a31515">"numbers only"</span>)<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">End</span> <span style="color:blue">If</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">End</span> <span style="color:blue">If</span><u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">Else</span> <span style="color:green">'if a digit compare index of decimal point (if it exists) and length of text</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">If</span> <span style="color:blue">Not</span> TextBox2.Text.LastIndexOf(Chr(46)) = -1 <span style="color:blue">Then</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">If</span> TextBox2.TextLength > TextBox2.Text.LastIndexOf(Chr(46)) + 2 <span style="color:blue">Then</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:green">'if length is over 2 spaces from the decimal point, dont allow </span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> e.Handled = <span style="color:blue">True</span><u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas"> MsgBox(<span style="color:#a31515">"only two decimal places allowed"</span>)<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">End</span> <span style="color:blue">If</span><u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">End</span> <span style="color:blue">If</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">End</span> <span style="color:blue">If</span><u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> <span style="color:blue">End</span> <span style="color:blue">Sub<u></u><u></u></span></span></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><b><span style="font-size:14.0pt"><img src="" alt="Description: signiture" height="86" width="194"><u></u><u></u></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US"><u></u> <u></u></span></b></p><p class="MsoNormal"><b><span lang="EN-US">"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" </span></b><span lang="EN-US">Joyce Cary.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><b><span style="font-size:14.0pt" lang="EN-US">Michael Wooldridge<u></u><u></u></span></b></p><p class="MsoNormal"><span style="font-size:12.0pt;color:#4f6228" lang="EN-US">Information Technology Coordinator<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt" lang="EN-US">Mornington Secondary College<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:8.0pt" lang="EN-US">1051 Nepean Highway, Mornington 3931<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:Wingdings" lang="EN-US">(</span><span style="font-size:8.0pt" lang="EN-US">: <a href="tel:03%2059700250" value="+61359700250" target="_blank">03 59700250</a> </span><span style="font-size:8.0pt;font-family:"Wingdings 2"" lang="EN-US">6</span><span style="font-size:8.0pt" lang="EN-US">: <a href="tel:03%2059700299" value="+61359700299" target="_blank">03 59700299</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:8.0pt" lang="EN-US"><a href="mailto:mwo@mornsc.vic.edu.au" target="_blank"><span style="color:blue">mwo@mornsc.vic.edu.au</span></a><u></u><u></u></span></p><p class="MsoNormal">
<span><u></u> <u></u></span></p><p class="MsoNormal"><b><span style="font-size:9.0pt">Important - </span></b><span style="font-size:9.0pt">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.<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p></div></div><br>_______________________________________________<br>
<a href="http://www.edulists.com.au" target="_blank">http://www.edulists.com.au</a> - FAQ, Subscribe, Unsubscribe<br>
IT Software Development Mailing List kindly supported by<br>
<a href="http://www.vcaa.vic.edu.au" target="_blank">http://www.vcaa.vic.edu.au</a> - Victorian Curriculum and Assessment Authority and<br>
<a href="http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html" target="_blank">http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html</a><br>
<a href="http://www.vitta.org.au" target="_blank">http://www.vitta.org.au</a> - VITTA Victorian Information Technology Teachers Association Inc<br>
<a href="http://www.swinburne.edu.au/ict/schools" target="_blank">http://www.swinburne.edu.au/ict/schools</a> - Swinburne University<br></blockquote></div><br><br clear="all"><br>-- <br>Mark Kelly<br>Manager of ICT, Reporting, IT Learning Area<br>
McKinnon Secondary College<br>McKinnon Rd McKinnon 3204, Victoria, Australia<br>Direct line / Voicemail: +613 8520 9085, Fax +613 9578 9253<br><a href="mailto:kel@mckinnonsc.vic.edu.au" target="_blank">kel@mckinnonsc.vic.edu.au</a> (don't use my Edumail account!)<br>
VCE IT Lecture Notes: <a href="http://vceit.com" target="_blank">http://vceit.com</a><br>Moderator: IT Applications Edulist<br><br>--<br>An E-flat, a C, and a G go into a bar. The bartender says, "I'm sorry, but I can't serve minors."<br>
<br><br>