Click here to Skip to main content
12,404,178 members (80,470 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi Guys

I have found a way to handle individual key press on textboxes but this way I have to code each textbox on its own.
I would like to know how to set a rule / Key Press event for all textboxes on the form.
So when the Key: “ ‘ “ is pressed do nothing.
Im getting errors when saving data using the character “ ‘ “
I hope this makes sense... : )
Posted 23-Sep-12 19:36pm
m@r10427
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can handle a Form event KeyPress[^] at the Form Level.
Sub Form1_KeyPress(ByVal sender As Object, _
    ByVal e As KeyPressEventArgs) Handles Me.KeyPress
 
    If e.KeyChar >= ChrW(48) And e.KeyChar <= ChrW(57) Then
        MessageBox.Show(("Form.KeyPress: '" + _
            e.KeyChar.ToString() + "' pressed."))
 
        Select Case e.KeyChar
            Case ChrW(49), ChrW(52), ChrW(55)
                MessageBox.Show(("Form.KeyPress: '" + _
                    e.KeyChar.ToString() + "' consumed."))
                e.Handled = True 
        End Select 
    End If 
End Sub
This answer is just a reference of How to: Handle Keyboard Input at the Form Level[^]


Hope it helps.!
--Amit
  Permalink  
v2
Comments
m@r10 24-Sep-12 1:52am
   
Thank you very much Amit.
Have you got any code in VB.NET
Very new at this.
_Amy 24-Sep-12 2:13am
   
I converted the code to VB.
m@r10 24-Sep-12 3:05am
   
The VB code above does not work for the entire form. Handles Me.KeyPress if i change the Me to textbox1 it works.... any other suggestions. I need this to work for all textboxes on the form
m@r10 24-Sep-12 3:05am
   
I have 60 textboxes on the form :(
Joan Murt 24-Sep-12 1:52am
   
5ed!
_Amy 24-Sep-12 2:13am
   
Thanks Joan.!
Ervin Pacibe 24-Sep-12 1:58am
   
use double ' when inserting
m@r10 24-Sep-12 2:36am
   
Hi Guys
The VB code above does not work for the entire form.
Handles Me.KeyPress

if i change the Me to textbox1.text it works....

any other suggestions.

I need this to work for all textboxes on the form
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Ami I got it to work:

set the KeyPreview property of the form to true

My Code..

Sub Form1_KeyPress(ByVal sender As Object, _
    ByVal e As KeyPressEventArgs) Handles Me.KeyPress
 
    If e.KeyChar = ChrW(39) Then
        MessageBox.Show(("Form.KeyPress: '" + _
            e.KeyChar.ToString() + "' pressed."))
 
        Select Case e.KeyChar
            Case ChrW(39)
                MessageBox.Show(("Form.KeyPress: '" + _
                    e.KeyChar.ToString() + "' consumed."))
                e.Handled = True
        End Select
    End If
End Sub

End If
End Sub

End If
End Sub



Thank you so much for the help

Cheers
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100