|
-
Sep 26th, 2008, 06:44 PM
#1
Thread Starter
Frenzied Member
[2005/2008] Change what user clicked on his/her keyboard.
Hello, i have always thought to cancel a key a user pressed, you can just write e.Handled = False. I was wrong...
Below is a quick example of making it so in a textbox if the user clicks the period/decimal it will not enter the period/decimal in the textbox it will enter a comma.
vb Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
e.SuppressKeyPress = True
SendKeys.Send(",")
End If
End Sub
You can use the above code as a layout, modify it etc...
Last edited by noahssite; Oct 31st, 2008 at 06:12 AM.
-
Sep 27th, 2008, 07:24 AM
#2
Re: Change what user clicked on his/her keyboard.
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
e.SuppressKeyPress = True
DirectCast(sender, TextBox).SelectedText = ","
End If
End Sub
This will insert a comma where ever the cursor is which is the same thing as using SendKeys, except you dont have any un-expected results that SendKeys has.
-
Sep 27th, 2008, 08:26 AM
#3
Thread Starter
Frenzied Member
Re: Change what user clicked on his/her keyboard.
 Originally Posted by JuggaloBrotha
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
e.SuppressKeyPress = True
DirectCast(sender, TextBox).SelectedText = ","
End If
End Sub
This will insert a comma where ever the cursor is which is the same thing as using SendKeys, except you dont have any un-expected results that SendKeys has.
Whats the downsides of SendKeys?\
Also when i did the TextBox that was only an example.
Place a label on your form and place the following.
vb Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
e.SuppressKeyPress = True
SendKeys.Send(",")
End If
Label1.Text = e.KeyCode.ToString
End Sub
Though seeing you on the forums a lot and reading your posts you probally know what it does..lol
Though in the case of a textbox i guess that would prove better.
Unless you can use DirectCast() on a form?
-
Sep 27th, 2008, 02:59 PM
#4
Re: Change what user clicked on his/her keyboard.
The downside of SendKeys is that if that code runs and a split second the form loses focus before your KeyDown event hits the 'SendKeys' line, then the comma will be sent to the window/program that has the focus. Now in this case it's probably never going to be an issue, but whenever using SendKeys, you have to keep this in mind. 99.999999999% of the time when you use SendKeys, there'll be another method that does the same thing anyways.
In your form example, there's no need to do a DirectCast because you already have the form's instance, it's the 'Me' keyword. In your TB KeyDown example, I did a DirectCast on the 'sender' so if there was more than 1 textbox that uses that KeyDown event code, then it'll work for any combination of TB's, that and if someone copies the code from here and put's it in their project, it's less code for them to change to make it work too.
-
Oct 31st, 2008, 05:52 AM
#5
New Member
Re: Change what user clicked on his/her keyboard.
not working with VS 2003, hic
-
Oct 31st, 2008, 06:15 AM
#6
Thread Starter
Frenzied Member
Re: [2005/2008] Change what user clicked on his/her keyboard.
oh.. i changed the title of this thread so people know its 2005/2008. Though whats wrong in 2003?
Are you copying the code directly?
You cant use the sub that i have already given. You need to copy the code inside the sub. Then place it in your Form1_KeyDown event etc...
I will try get a 2003 version of my code if that's not the problem.
Also Welcome to the forums
-
Oct 31st, 2008, 08:17 AM
#7
Re: [2005/2008] Change what user clicked on his/her keyboard.
I'm not sure if VS 2003 has the e.SupressKeyPress property in the keydown event or not, the rest of it will work in VS 2003.
-
Oct 31st, 2008, 05:21 PM
#8
Thread Starter
Frenzied Member
Re: [2005/2008] Change what user clicked on his/her keyboard.
 Originally Posted by JuggaloBrotha
I'm not sure if VS 2003 has the e.SupressKeyPress property in the keydown event or not, the rest of it will work in VS 2003.
So what would be an alternate?
-
Nov 3rd, 2008, 09:33 PM
#9
Member
Re: [2005/2008] Change what user clicked on his/her keyboard.
Hi noah. These codes are meant for textboxes? I couldn't really understand how i can manipulates my codes to make it work for datagridview.
-
Nov 4th, 2008, 08:50 AM
#10
Thread Starter
Frenzied Member
Re: [2005/2008] Change what user clicked on his/her keyboard.
Oh.. i thought it would work for a DataGridView though i dont use DataGridViews much so i wasnt 100% sure..
If i figure a means of doing it for a DataGridView i will post it.
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
|