DotNet Visual Basic: Yet another question

  • kidz14 / 104 / Fri, 27 Mar 2009 21:46:00 GMT / Comments (13)
  • how would i make it so that when i press enter in a txt box it dose lets say LoadStats()

    this is what i have dosen't work though

    Private Sub txtname1_Click(ByVal sender As System.Object, ByVal e As__
    System.EventArgs) Handles txtname1.Click
    If txtname1.Pressed = ("{ENTER}") Then
    LoadStats()
    Else
    End If
    End Sub
  • Keywords:

    dotnet, visual basic, vb, .net

  • http://dotnet.itags.org/dotnet-visual-basic/172/«« Last Thread - Next Thread »»
    1. You need to handle a keyboard event instead of a mouse event if you want to check for a particular key. Handle the KeyDown event and compare the e.KeyCode property to Keys.Enter. Intellisense will help you and you obviously already know how to create an event handler in the first place.

      jmcilhinney | Tues, 04 Dec 2007 14:32:00 GMT |

    2. I think you're going to want to use the KeyPress event rather than the click event.

      Some might suggest using the LostFocus event, depending on what you are doing, but I would stay away from that event entirely, it is unreliable in my experience.

      shaggyhiker | Tues, 04 Dec 2007 14:33:00 GMT |

    3. Oh yeah, KeyDown, that's it.

      shaggyhiker | Tues, 04 Dec 2007 14:34:00 GMT |

    4. this is what i have tried

      Private Sub txtname1_Enter(ByVal sender As Object, ByVal e As__
      System.EventArgs) Handles txtname1.Enter
      LoadStats()
      End Sub

      and

      Private Sub txtname1_KeyDown(ByVal sender As Object, ByVal e As__
      System.Windows.Forms.KeyEventArgs) Handles txtname1.ModifiedChanged
      LoadStats()
      End Sub

      i think keydown don't work because it's if i hold down the key i want it so that as soon as i press enter it dose the LoadStat() sub

      and it don't work little more detail plz

      kidz14 | Tues, 04 Dec 2007 14:35:00 GMT |

    5. Your code indicates that you are handling the ModifiedChanged event and not the KeyDown event. It doesn't matter what the procedure is called, only what's in the Handles clause. Also, I've already said that you need to test the e.KeyCode property to make sure that ENTER was the key that was depressed and there's no sign of that in your code.

      jmcilhinney | Tues, 04 Dec 2007 14:36:00 GMT |

    6. Look that one up, it has nothing to do with keypresses, but is the first event sent to the textbox when it gets focus.

      What you want is to handle the KeyDown event. If you put the cursor in the sub you posted, you should be able to pick it off a list in the dropdown on the right above the code page. The keypress event receives a KeyPressEventArgs argument that has a KeyCode member. That's what you need.

      KeyDown is also an option, but if the user were to hold down the key, the KeyDown event should fire every time the key repeats (according to MSDN).

      shaggyhiker | Tues, 04 Dec 2007 14:37:00 GMT |

    7. Hey, no fair, you edited your post as I was writing.

      shaggyhiker | Tues, 04 Dec 2007 14:38:00 GMT |

    8. okay for all you people that waisted my time here the anwser if you all needed it

      Private Sub txtname1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtname1.KeyPress
      If e.KeyChar = Chr(13) Then
      LoadStats()
      End If
      End Sub
      End Class

      by the way i was just kidding i love you all for all your help by the way jmcilhinney i think you actually tried to tell me to do that but i didn't quit understand you should try pasting code every now and then

      kidz14 | Tues, 04 Dec 2007 14:39:00 GMT |

    9. And yet you still chose to do it differently: Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
      If e.KeyCode = Keys.Enter Then
      LoadStats()
      End If
      End SubIf you use the KeyPress event then if the user holds the Enter key down the code will be executed multiple times, whereas with the KeyDown event it will be executed only once. I choose not to post code in many situations that aren't too complex because I want to encourage people to think for themselves a little. I did say handle the KeyDown event and compare e.KeyCode to Keys.Enter. That seems pretty explicit to me.

      jmcilhinney | Tues, 04 Dec 2007 14:40:00 GMT |

    10. i know but i didn't quite know how to use the e. thingy untill now but thanks learned something new cause of you :D

      kidz14 | Tues, 04 Dec 2007 14:41:00 GMT |

    11. This seems surreal. I was reading something on MSDN about what I thought was KeyPress, but when I went back to confirm it, I have a different set of text. The current thing I am reading says that KeyPress only receives character keys, so Enter would not be used. KeyDown is the ONLY one that will work. I wonder what I was reading before that showed that KeyDown would repeat, and KeyPress would not?

      I believe Chr() is in the VisualBasic namespace, which some people here are opposed to. It is also ASCII, while .NET is fundamentally Unicode (though you can do either one, the emphasis is on Unicode). For those reasons, jmcilhinney (whoes name I will butcher in the spelling, no doubt) has a slightly better solution.

      shaggyhiker | Tues, 04 Dec 2007 14:42:00 GMT |

    12. keypress actually dose work so ya check b 4 you talk

      kidz14 | Tues, 04 Dec 2007 14:43:00 GMT |

    13. I don't recall posting that the KeyPress event wouldn't work. What I said was that it will be raised multiple times if the user holds the key down, whereas KeyDown will only be raised once each time the key is depressed. If it is acceptable or desirable that your LaodStats method be called multiple times in that circumstance then using the KeyPress event is fine. If not then you should be handling the KeyDown event as I suggested. Also, to stick with Unicode you should use ChrW or, preferably, Convert.ToChar.

      jmcilhinney | Tues, 04 Dec 2007 14:45:00 GMT |