开发者

How to use the textbox1_keydown?

How do I use key_down event in my vb.net? I have seen so many codes on google but none of them working I don't understand where its going wrong

  1. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx#Y600
  2. http://www.daniweb.com/software-development/vbnet/threads/114278
  3. http://forums.devshed.com/net-development-87/keypress-allow-only-letters-and-numbers-with-max-lenght-of-528176.html
  4. http://social.msdn.microsoft.com/Forums/pl-PL/Vsexpressvb/thr开发者_Python百科ead/aab1d64c-a9dc-4dd2-8d2f-83a414e9c909
  5. http://www.bigresource.com/VB-Lock-textbox-so-the-user-only-can-enter-numbers-a8mxDB7ouq.html
  6. http://forums.devx.com/archive/index.php/t-96951.html

Above are the links I googled and they are many links =I have googled to work on key_down events on vb.net But my god its aint working I dont know what to do.

1) How do I use textbox1_keydown event are there any necessary steps taken before using it ( why its aint workin?)

2) can anyone post a sample of these "a textbox that allows only numbers from user using key_down"

3) I see people using e.keychar but in my vb.net(2008) I dont have that keyword I guess e.keycode must be used is that right? or e.keyvalue?

4) I see e.keycode = keys.A but I need to accept 'a' not "A" How do I specify a number or letter with their ascii value ?


Are you not able to use KeyPress for some reason?

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Char.IsDigit(e.KeyChar) Then
        e.Handled = True    'Prevents all numbers from from being placed
    End If

    If Convert.ToInt32(e.KeyChar) = 97 Then
        e.Handled = True    'Prevents "a" from being placed
    End If
End Sub

This should do what you need. You can find a list of ASCII codes here if you don't have one already. Take a look at the intellisense for Char. and check out all the useful things you can filter on. You can also specify exact ASCII values by converting them to int.


It sounds like you are getting a little confused between KeyDown and KeyPress.

I think this is what you are looking for:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
  If Not Char.IsNumber(e.KeyChar) Then
    e.Handled = True
  End If
End Sub

If you are trying to enter only numbers and the lower case "a" then:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
  If Not (Convert.ToInt32(e.KeyChar) = Asc("a") Or Char.IsNumber(e.KeyChar)) Then
    e.Handled = True
  End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜