locked
KeyPress - ASCII values???

    คำถาม

  • What would be the best way to indicate to the application WHAT key was pressed (using ASCII values)?

     

    For example, I have a DataGrid.  If the Tab key is pressed I want to move to a textbox control.  If the Enter key is pressed I want to retrieve the current row's data, populate variables and move to a textbox control that has the selected data displayed.

     

    I thought that I would do that with the KeyPress method on my data grid using if statements for CHr(9) and CHr(13) but it doesn't seem to work right.

     

    Thanks for any help ya'll have to offer.

    28 มีนาคม 2550 19:23

คำตอบ

  • Hi,

     

    Add formName_KeyUp Event on the Form, after setting the KeyPreview Property to True;

     

    Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

    If e.KeyCode.Equals(e.KeyCode.Tab) Then

    MessageBox.Show("You pressed Tab")

    End If

    End Sub

     

    If you want to add this KeyUp Event on GridData, then just add KeyUp Event and write the same code there as well. It would work.

     

    Kind regards,

    Arif

     

    28 มีนาคม 2550 21:09
  •  tmfoust wrote:

    Great suggestions - thanks.  But I am still not getting the desired effect.

     

    I think perhaps I need to be a little more descriptive.  I am building this app so that it can be used without a mouse.

     

    I have a Datagrid that I populate with a dataadapter.  Once the screen is shown I want the users to be able to use the arrow keys to move vertically through the datagrid (this works).  Then I want the users to be able to press Tab to move to the text box I have for data entry OR press Enter to select the current record in the Datagrid and automatically populate my textbox and move to my continue button.

     

    What is currently happening is - I get my populated Datagrid.  I hit the Tab key and the cursor moves horizontally through the fields in the datagrid.  I hit the Enter key and nothing happens.

     

    Is there even a way to do what I want or should I start looking at alternative delivery methods?

    Thanks again!

     

    OK TM... At first I was scratching my head wondering why the examples given aren't clear enough. Assuming you're a smart guy I decided to reproduce your description in a Windows application. I was SHOCKED to find that there seems to be a condition with the DataGrid that prevents the form KeyPress, KeyDown, and KeyUp events from firing when the StandardTab property is set to true.

     

    Setting the StandardTab property to True will prevent the form from raising any key events. I'de call this a bug, but without the source code for the components I can't say for sure.

     

    As a work around you can set the StandardTab property to False and implement the examples listed above. It's important to note that even when the StandardTab property is set to false, the DataGrid will still process the tab key (another bug). Here's a quick example of what your code should look like after you set the StandardTab property to False.

     

    If Me.TableDateDataGridView.Focused Then

    If e.KeyChar = Chr(Keys.Tab) Then

    Me.TextBox1.Focus()

    e.Handled = True

    End If

    End If

     

    Good luck,

    Johnny

     

    30 มีนาคม 2550 15:14
  • Ah, OK...it truly is a DataGrid, then...I'm also using 2005, so it may be difficult to establish a solution form someone other than those using VS2003......this is one gipe I have about the forums: difficulty in separating 2003 and 2005 situations...

     

    btw...what are the issues betwen .NET 1.1 and 2.0. I've heard this mentioned before, but the only issues are that the programmers don't want to do it, rathe than there being actual issues......perhaps there are, I don't know...

     

     

    30 มีนาคม 2550 18:58
  • Just taking a look in some threads about ADO .net and I saw your pain with the Keypress/KeyUp/KeyDown events and .net.
    I don't know if it is your problem but I felt the pain with the same events and discover the the Enter key ou Return don't fires any of the event other than PreviewKeyDown...
    So I move the statements that were testing for the return key pressed to the event PreviewKeyDown

    Like this:

      Private Sub TxtBairroNF_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TxtBairroNF.PreviewKeyDown
        If e.KeyCode = Keys.Return Then
          .................
        End If
      End Sub

    I don't know if this applies to VS2003 but maybe can help you.
    Marcio.
    30 มีนาคม 2550 19:08

ตอบทั้งหมด

  • Sounds like you're on the right track. Have you set the KeyPreview property to true on the form? Also the application will process the keyPress unless you set e.Handled to true. Meaning you must tell the application not to process the Tab key.

     

    Johnny 

    28 มีนาคม 2550 19:56
  • Thanks, but that's not quite it.

     

    What is happening is - when I hit the Tab key, the cursor moves through the fields in my datagrid.

    When I hit the Enter key, nothing happens.

    28 มีนาคม 2550 20:53
  • Hi,

     

    Add formName_KeyUp Event on the Form, after setting the KeyPreview Property to True;

     

    Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

    If e.KeyCode.Equals(e.KeyCode.Tab) Then

    MessageBox.Show("You pressed Tab")

    End If

    End Sub

     

    If you want to add this KeyUp Event on GridData, then just add KeyUp Event and write the same code there as well. It would work.

     

    Kind regards,

    Arif

     

    28 มีนาคม 2550 21:09
  •  

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Set the keyPreview property anyway.

    Me.KeyPreview = True

    End Sub

    Private Sub formLevel_KeyDown(ByVal sender As System.Object, _

    ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

    'If you type in textBox2 instead and

    'make textbox1 large enough to see 3 lines of text then you'll

    'see the keyData line shows if the Alt , Ctrl or Shift keys are pressed too. :-)

    Dim keyCodeString As String = e.KeyCode.ToString

    Dim keyData As String = e.KeyData.ToString

    Dim keyValue As String = e.KeyValue.ToString

    'Set the ASCII value in a variable.

    Dim keyAscii As Integer = e.KeyValue

    'Show the key pressed in a textBox.

    TextBox1.Text = "KeyCode = " & keyCodeString & vbCrLf & "KeyData = " & keyData & vbCrLf & "KeyValue = " & keyValue

    End Sub

    End Class

     

     

    Regards

    S_DS

     

    29 มีนาคม 2550 0:04
  • To move the focus to another control when the Enter key is pressed in a textbox, type the following in the KeyPress event.  You may expand on this example to do exactly what you need.

     

    :     If e.KeyChar = Chr(13) Then Textbox2.Focus()

     

    E

     

     

    29 มีนาคม 2550 0:20
  • Great suggestions - thanks.  But I am still not getting the desired effect.

     

    I think perhaps I need to be a little more descriptive.  I am building this app so that it can be used without a mouse.

     

    I have a Datagrid that I populate with a dataadapter.  Once the screen is shown I want the users to be able to use the arrow keys to move vertically through the datagrid (this works).  Then I want the users to be able to press Tab to move to the text box I have for data entry OR press Enter to select the current record in the Datagrid and automatically populate my textbox and move to my continue button.

     

    What is currently happening is - I get my populated Datagrid.  I hit the Tab key and the cursor moves horizontally through the fields in the datagrid.  I hit the Enter key and nothing happens.

     

    Is there even a way to do what I want or should I start looking at alternative delivery methods?

    Thanks again!

    30 มีนาคม 2550 14:34
  •  tmfoust wrote:

    Great suggestions - thanks.  But I am still not getting the desired effect.

     

    I think perhaps I need to be a little more descriptive.  I am building this app so that it can be used without a mouse.

     

    I have a Datagrid that I populate with a dataadapter.  Once the screen is shown I want the users to be able to use the arrow keys to move vertically through the datagrid (this works).  Then I want the users to be able to press Tab to move to the text box I have for data entry OR press Enter to select the current record in the Datagrid and automatically populate my textbox and move to my continue button.

     

    What is currently happening is - I get my populated Datagrid.  I hit the Tab key and the cursor moves horizontally through the fields in the datagrid.  I hit the Enter key and nothing happens.

     

    Is there even a way to do what I want or should I start looking at alternative delivery methods?

    Thanks again!

     

    OK TM... At first I was scratching my head wondering why the examples given aren't clear enough. Assuming you're a smart guy I decided to reproduce your description in a Windows application. I was SHOCKED to find that there seems to be a condition with the DataGrid that prevents the form KeyPress, KeyDown, and KeyUp events from firing when the StandardTab property is set to true.

     

    Setting the StandardTab property to True will prevent the form from raising any key events. I'de call this a bug, but without the source code for the components I can't say for sure.

     

    As a work around you can set the StandardTab property to False and implement the examples listed above. It's important to note that even when the StandardTab property is set to false, the DataGrid will still process the tab key (another bug). Here's a quick example of what your code should look like after you set the StandardTab property to False.

     

    If Me.TableDateDataGridView.Focused Then

    If e.KeyChar = Chr(Keys.Tab) Then

    Me.TextBox1.Focus()

    e.Handled = True

    End If

    End If

     

    Good luck,

    Johnny

     

    30 มีนาคม 2550 15:14
  • Setting StandardTab true doesn't block the keyDown events on the DataGridView (and since the StandardTab was mentioned, I don't believe a DataGrid has such a thing, i presume we are referring to a DataGridView...).

     

    All you would need to then do is suppress the keypress in the DataGridView when the enter key is pressed (and handle it, of course) to prevent it from going to the next line.

     

    (apologies if we are talking about the older DataGrid control...)

    30 มีนาคม 2550 16:13
  • Thanks, Johnny and Stephen.

     

    1st, Johnny, I'm a girl - but that's neither here nor there.

     

    2nd, I am working in VB.Net 2003 so am indeed working with the older DataGrid.  And, as Stephen stated, the DataGrid does not have the StandardTab Property.

     

    Honestly, I, myself am amazed that these solutions aren't working.  I have taxed my brain because I am sure I have done something similar before, but for the life of me, I cannot remember.

     

    I think it may just be time to look at an alternate delivery method.  Maybe a listbox populated from the Dataset...

    Thanks again.

    30 มีนาคม 2550 16:37
  • DOH! Sorry for the assumption. It's always cool to meet a fellow programmer of the oppisite sex.

     

    I'm using VS 2005 so my examples won't be of much help to you Sad

     

    Johnny

    30 มีนาคม 2550 17:33
  • That's kind of what I'm afraid of.  Most people, I'm guessing, will be using hte more recent versions of VS.  Unfortunately, the company I work for has not yet upgraded because of issues between .Net 1.1 and .Net 2.0.

     

    If you happen to find anything regarding the DataGrid from 2003 - please feel free to pass it along.

    30 มีนาคม 2550 18:15
  • Ah, OK...it truly is a DataGrid, then...I'm also using 2005, so it may be difficult to establish a solution form someone other than those using VS2003......this is one gipe I have about the forums: difficulty in separating 2003 and 2005 situations...

     

    btw...what are the issues betwen .NET 1.1 and 2.0. I've heard this mentioned before, but the only issues are that the programmers don't want to do it, rathe than there being actual issues......perhaps there are, I don't know...

     

     

    30 มีนาคม 2550 18:58
  • Just taking a look in some threads about ADO .net and I saw your pain with the Keypress/KeyUp/KeyDown events and .net.
    I don't know if it is your problem but I felt the pain with the same events and discover the the Enter key ou Return don't fires any of the event other than PreviewKeyDown...
    So I move the statements that were testing for the return key pressed to the event PreviewKeyDown

    Like this:

      Private Sub TxtBairroNF_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TxtBairroNF.PreviewKeyDown
        If e.KeyCode = Keys.Return Then
          .................
        End If
      End Sub

    I don't know if this applies to VS2003 but maybe can help you.
    Marcio.
    30 มีนาคม 2550 19:08
  • Stephen, I quite agree.  However, what I have seen in production as well as noted in MS documentation is that the issues between .Net versions is supportablity.  Many application written in .Net 1.0 and 1.1 are self supporting and do not have the correct setting and code to function correctly cross platform.  It is the same with 1.1 and 2.0.  Sad, but true!

     

    Marcio, unfortunately I don't think your suggestion applies to 2003.

     

    The sad thing to note in all this is that if I weren't trying to make this a mouseless application - I wouldn't be having these issues!

    Thanks again everyone!

    30 มีนาคม 2550 19:41
  •  SJWhiteley wrote:
     

     

    btw...what are the issues betwen .NET 1.1 and 2.0. I've heard this mentioned before, but the only issues are that the programmers don't want to do it, rathe than there being actual issues......perhaps there are, I don't know...

     

     

     

    Sj,

     

    I'm not too familar with issues between 1.1 and 2.0, but I can say that I prefer the 2.0 components. I don't mean to be bias, but when I look for the functionality I need I often see red text at the top of the help page stating its a new component for 2.0. The new bindingSource component seems like a must, I'm not sure how you guys got by without it in 1.1.

     

    I wrote 1 application in 1.1 and struggled through it. Since then I've written 15 applications in 2.0 and loved every minute of it. If memory serves, I spent a lot of time converting between object data types in 1.1, where as 2.0 seems to have a library that requires less conversion. Meaning the objects seem to be designed to work together rather then employ thier own technique for resolving problems. I haven't written any ASP.NET applications in 1.1, but I can honestly state that the 2.0 architecture is superb (with a few major issues). I'm probably way off in my observations given my lack of experience with 1.1.

     

    Johnny

    2 เมษายน 2550 12:50