Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 10th, 2008, 08:33 AM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default simple code, solve many problems

I was trying to restrict the data entered in a text box to only decimal point numbers and realised that .net doesn't really provide for this, or at least not in any easy way so i messed arount with a few things and eventually came up with the following code which i hope others will find useful. If anyone else has an easier way please let me know.

The first three lines are just to show some other possiblities of this kind of coding

        ' allow enter key to perform same as tab button

        If (AscW((CType(e.KeyChar, String)))) = 13 Then
            txtRate.Focus()
        End If

        'Allow Esc key to perform exit button

        If (AscW((CType(e.KeyChar, String)))) = 27 Then
            btnExit.PerformClick()
        End If

        'Allow BackSpace key to delete text

        If (AscW((CType(e.KeyChar, String)))) = 8 Then
            txtPrinciple.Text = ""
        End If





        'Allow only numbers and decimal points to be entered by user

        If Not (AscW((CType(e.KeyChar, String)))) = 46 And e.KeyChar < "0" Or e.KeyChar > "9" Then
            e.Handled = True

        End If

Hope this helps someone else from hours of frustration


 
Old June 10th, 2008, 12:35 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If you want to do something like this (which is usually frowned on; after all this is not really a chat room) you should use a subject line that will cause this post to be found through searches...

There is no reason to parenthesize the entire test statement...
Code:
    If (AscW((CType(e.KeyChar, String)))) = 13 Then

    ' can be

    If AscW(CType(e.KeyChar, String)) = 13 Then
    (for instance.)


This could be a lot easier to read. Every test uses the same setup...
Code:
    Dim Inp As Integer = AscW(CType(e.KeyChar, String)) 

    Select Case Inp
        Case 13  ' Allow enter to perform same as tab
            txtRate.Focus()

        Case 27  ' Allow Esc key to perform exit btn
            btnExit.PerformClick()

        Case 8   ' Allow BkSpc key to delete text
            txtPrinciple.Text = ""

        ' Allow only nums & DPs to be entered
        Case 46  ' Do nothing.  Effectively the "Not" from the post

        Case Else 
            If e.KeyChar < "0" _
            Or e.KeyChar > "9" Then e.Handled = True

    End Select
 
Old September 8th, 2008, 06:17 AM
Authorized User
 
Join Date: Jun 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This post is already old, but you never know, maybe other people are looking at older posts too :)

To make it even shorter:

This...
Code:
If e.KeyChar < "0" _
            Or e.KeyChar > "9" Then e.Handled = True
...could be
Code:
If Char.IsDigit(e.KeyChar) Then e.Handled = True





Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple ASP page problems - FIXED Andrew.Berry Classic ASP Basics 2 January 11th, 2008 10:19 AM
Very Simple code Tal1481 Beginning VB 6 9 January 29th, 2007 11:12 AM
Very Simple code Tal1481 Visual Basic 2005 Basics 1 January 25th, 2007 07:15 AM
How to solve this problems? hsfoo82 PHP How-To 1 February 24th, 2005 12:24 AM
Can you solve the simple problem ? kishore_peddi C# 2 November 3rd, 2003 09:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.