-
Jan 21st, 2012, 01:23 PM
#1
Thread Starter
New Member
Disable Keys
Hi friends,
I am new to VB.just now started to coding . I am designing one application in that one text box should allow only (A-Z,0-9,/).when other keys pressed it wont work i.e., i have to lock when other key pressed.
please guide me and give logic/code to complete above task.
-
Jan 21st, 2012, 03:16 PM
#2
Re: Disable Keys
Try this
vb Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Select Case e.KeyChar Case "A" To "Z" ' do nothing Case "0" To "9" ' do nothing Case "\" ' do nothing Case Else ' suspend other keys e.KeyChar = "" End Select End Sub
-
Jan 23rd, 2012, 08:54 AM
#3
Re: Disable Keys
@ecalt:
What version of Visual Basic do you use?
-
Jan 23rd, 2012, 08:58 AM
#4
Re: Disable Keys
Originally Posted by 4x2y
Try this
vb Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "A" To "Z"
' do nothing
Case "0" To "9"
' do nothing
Case "\"
' do nothing
Case Else
' suspend other keys
e.KeyChar = ""
End Select
End Sub
That's not even needed, the only method you'd need to use is:
That's it.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jan 23rd, 2012, 09:11 AM
#5
Re: Disable Keys
Originally Posted by AceInfinity
That's not even needed, the only method you'd need to use is:
That's it.
There must be some checks because not all keys need to suspended.
I have used e.KeyChar = "" because e.SupressKeypress is available only under KeyUp/Down events.
-
Jan 23rd, 2012, 08:25 PM
#6
Re: Disable Keys
Originally Posted by 4x2y
There must be some checks because not all keys need to suspended.
I have used e.KeyChar = "" because e.SupressKeypress is available only under KeyUp/Down events.
That's true, I wasn't referring to his exact code in that statement though, but with your code, I would do something like this, which is a bit more efficient:
Code:
Dim MyChars As String = "/" & vbBack
e.Handled = Not Char.IsLetterOrDigit(e.KeyChar) AndAlso MyChars.IndexOf(e.KeyChar) = -1
The way you have it set up now, it won't accept input if the char value is lowercase, only uppercase.
Last edited by AceInfinity; Apr 19th, 2012 at 10:36 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jan 24th, 2012, 12:06 PM
#7
Re: Disable Keys
Why is this thread in the API forum? It really belongs in VB.Net.
-
Apr 19th, 2012, 08:38 PM
#8
Frenzied Member
Re: Disable Keys
This is .net code
The guy asked for API. If you can't give him an API solution, your solution probably isn't wanted. Most people in the API forum have VB6, but want to do the cool things of .net so they use API to get most similar functions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|