keypress question

Posted on 2006-11-19
Last Modified: 2010-04-23
Good Morning Troops

I have the following in the Keypress event for a textbox: I would like for it to handle other textboxs. As written it refers to only txtRentalpmt.text which imakes the handles statement ( handles anothertextbox.kepress) dependent on txtRemtalPmt.text

 Dim IntegerOnly As Boolean
        'Dim PositiveOnly As Boolean
        Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar) ' NOTE: used as a flag when = 0 to mark bad values
        Select Case KeyAscii
            Case 48 To 57, 8, 13 ' Digits 0 - 9, Backspace, CR.
            Case 46 ' Period (decimal point).
                If IntegerOnly Then
                    KeyAscii = 0
                    'If we already have one period, throw it away
                    If InStr(txtRentalPmt.Text, ".") <> 0 Then         '<-----------------Cant be dependent on this textbox
                        KeyAscii = 0
                    End If
                End If
            Case Else ' Provide no handling for other keys.
                KeyAscii = 0
        End Select
        If KeyAscii = 0 Then
            e.Handled = True
            e.Handled = False
        End If
Question by:stephenz43
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 10

Expert Comment

ID: 17975764
Handle all the textboxes in the handles part like:

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

    End Sub
LVL 10

Expert Comment

ID: 17975768
and change :



LVL 10

Accepted Solution

Kinger247 earned 500 total points
ID: 17975778
Sorry to quick on the enter key , anyway ...

so the statement will look like ..

If InStr(directcast(sender,textbox).text, ".") <> 0 Then      
  KeyAscii = 0
End If

or change it to:

If DirectCast(sender, TextBox).Text.Contains(".") Then
  KeyAscii = 0
End If

Author Comment

ID: 17975820
Thank you
LVL 10

Expert Comment

ID: 17975824
no problem.

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question