locked
keypress event of vb6 to vb.net?

    Question

  •  

    how do i use the keypress event of vb6 to a vb.net working code?

    i'm new to vb.net and need help in restricted chars typed into a textbox a code i used before for a vb6 app was this.

    Private Sub txtBox1_keypress(ByVal KeyAscii As Integer)

    Select Case KeyAscii

    Case 45, 46, 47, 48 To 57, 65 To 90, 97 To 122 Case Else

    KeyAscii = 0

    End Select

    End Sub

     

     

    i think it was something like this.. can anyone help in writing this code in vb.net?

    sorry im very new to vb.net

     

    Sunday, October 14, 2007 3:30 AM

Answers

  •  

    Private Sub Textbox1_KeyPress(ByVal sender As Object, _

            ByVal e As System.Windows.Forms.KeyPressEventArgs) _

            Handles TextBox1.KeyPress

    'The following properties must be set thusly:

    ' Textbox1.Multiline = True

    ' Textbox1.AcceptsReturn = True

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

    ' Code to be executed goes here.

    e.Handled = True

    End If

    End Sub

    Sunday, October 14, 2007 3:54 AM
  • You have several options.  I see that the only characters you want to accept are all the letters and digits, plus the - and . symbols.  To use the Select Case block that emulates your old code, you could do this:

     

    Code Block

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Select Case e.KeyChar

       Case Chr(45) To Chr(57), Chr(65) To Chr(90), Chr(97) To Chr(122)

          e.Handled = False

       Case Else

          e.Handled = True

    End Select

    End Sub

     

     

     

    However, a cleaner way of doing it in VB.Net is to use a new method for the character with the handler:

     

    Code Block

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Not Char.IsLetterOrDigit(e.KeyChar) Then e.Handled = True

    If e.KeyChar = "-" Or e.KeyChar = "." Then e.Handled = False

    End Sub

     

     

     

    Sunday, October 14, 2007 6:56 AM

All replies

  • Can anyone help me on how to write this code which is from vb6 i think to a vb.net?

    Sorry i'm just new to vb.net

     

    Private Sub txtBox1_KeyPress(ByVal KeyAscii As Integer)

    Select Case KeyAscii

    Case 45, 46, 47, 48 To 57, 65 To 90, 97 To 122

    Case Else

    KeyAscii = 0

    End Select

    End Sub

     

    as you may see i have to restrict inputs in textboxes to a certain chars only.

    Thanks in advance!

    Sunday, October 14, 2007 3:04 AM
  •  

    Private Sub Textbox1_KeyPress(ByVal sender As Object, _

            ByVal e As System.Windows.Forms.KeyPressEventArgs) _

            Handles TextBox1.KeyPress

    'The following properties must be set thusly:

    ' Textbox1.Multiline = True

    ' Textbox1.AcceptsReturn = True

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

    ' Code to be executed goes here.

    e.Handled = True

    End If

    End Sub

    Sunday, October 14, 2007 3:54 AM
  • Code Block

    Private Sub Txtbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Txtbox1.KeyPress

      

    Try

    Dim keyascii As Integer = Asc(e.KeyChar)

    Select Case keyascii

    Case 45, 46, 47, 48 To 57, 65 To 90, 97 To 122

    Case Else

    keyascii = 0

    End Select

    Catch ex As ArgumentException

    MsgBox("wrong argument")

    End Try

    End Sub

     

     

     

     

    Sunday, October 14, 2007 4:43 AM
  • You have several options.  I see that the only characters you want to accept are all the letters and digits, plus the - and . symbols.  To use the Select Case block that emulates your old code, you could do this:

     

    Code Block

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Select Case e.KeyChar

       Case Chr(45) To Chr(57), Chr(65) To Chr(90), Chr(97) To Chr(122)

          e.Handled = False

       Case Else

          e.Handled = True

    End Select

    End Sub

     

     

     

    However, a cleaner way of doing it in VB.Net is to use a new method for the character with the handler:

     

    Code Block

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Not Char.IsLetterOrDigit(e.KeyChar) Then e.Handled = True

    If e.KeyChar = "-" Or e.KeyChar = "." Then e.Handled = False

    End Sub

     

     

     

    Sunday, October 14, 2007 6:56 AM
  • The same question was posted in the Visual Basic Express forum by someone with a similar name.  Was that you?.  Please see the solution I posted there -- it addresses your code directly:

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2266863&SiteID=1

     

    Please avoid posting the same question in more than one forum, or using more than one name.

     

    Sunday, October 14, 2007 7:06 AM
  • thank you so much to the both of you!
    Sunday, October 14, 2007 9:30 AM
  • i got an error
    it seems that the System.Windows.Forms.KeyPressEventArgs is not defined what should i import to fix this?
    i'm sorry i think its because im using visual web developer but coding it in vb.net so i post in this forums sorry for the trouble
    Sunday, October 14, 2007 10:00 AM
  •  

    This is a demonstration on why these issues are referred to www.asp.net.

     

    Visual Web developer in not going to even allow the System.Windows.Forms Classes or subclassess because they won't run in IIS which serves a page to the net. The asp environment is so different that it's almost a different technology even though your language may be VB.

     

    By policy, we don't handle these questions here because the expertise is "there".

    Sunday, October 14, 2007 2:02 PM
  • thanks for being patient with me   i'll go look into asp.net for this
    Monday, October 15, 2007 3:11 AM