Results 1 to 10 of 10

Thread: [2005/2008] Change what user clicked on his/her keyboard.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    [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:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.         If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
    3.             e.SuppressKeyPress = True
    4.             SendKeys.Send(",")
    5.         End If
    6.     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.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Change what user clicked on his/her keyboard.

    Quote 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:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
    3.             e.SuppressKeyPress = True
    4.             SendKeys.Send(",")
    5.         End If
    6.         Label1.Text = e.KeyCode.ToString
    7.     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?

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    New Member
    Join Date
    Oct 2008
    Posts
    2

    Re: Change what user clicked on his/her keyboard.

    not working with VS 2003, hic

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005/2008] Change what user clicked on his/her keyboard.

    Quote 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?

  9. #9
    Member
    Join Date
    Nov 2008
    Posts
    63

    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.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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
  •  



Click Here to Expand Forum to Full Width