Private Sub txtPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPassword.KeyPress
Dim NotAllowed As String = "~`@%^&+={[}]()!:,;'><?/|\-.#+()_$*"

    If e.KeyChar = ChrW(Keys.Enter) Then
        e.Handled = True
    End If

    If e.KeyChar <> ControlChars.Back = True Then
        If NotAllowed.IndexOf(e.KeyChar) = -1 = False Then
            e.Handled = True
        End If
    End If
End Sub

hi guys i just wanna ask if this code is true and makes the enter key null

ow, so its true, but why when i test i still key enter is not stop,
btw i wanna ask if enter key can be prevented from LoginForm or not?

If you don't want anything happen on the key enter just don't use it.

 Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim NotAllowed As String = "~`@%^&+={[}]()!:,;'><?/|\-.#+()_$*"
        If e.KeyChar <> ControlChars.Back = True Then
            If NotAllowed.IndexOf(e.KeyChar) = -1 = False Then
                e.Handled = True
            End If
        End If
    End Sub

Seems to work fine; are you setting the Handle to the right control?

Private Sub txtPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles [Your_Control_Name].KeyPress

@minimalist
well what i want is that when someone press the enter key for loginform it will not work
@atove
the control name is the txtPassword?

As per the code you've posted above; thats right. Unless you've named the textbox control for your password field to something else. Otherwise i've tested this and its handling enter and preventing it from returning a line within a multiline textbox.

Is there perhaps another sub thats catching the enter keypress event when its pressed on the loginform?

Ahh. By default your Login button is focused. So when enter is pressed its clicking the button. You need to handle the enter press event on the login button. But instead you could set the default focus to be one of your textbox controls instead. Just set the TabIndex property of one of those to 0 which would also remove the highlighting as the button would not have default focus.

I don't know of a way off the top of my head to remove the highlighting on the button. Its apart of the windows interface to show that a button has focus. Really its something that should be shown to indicate to the user what they currently have focused.

This question has already been answered. Start a new discussion instead.