Receive ENTER KEY in textbox

laroberts

Freshman
Joined
Jun 29, 2005
Messages
34
I have a form that is setup to ask for a user anme and then a password. I would like it if the user could just press enter as soon as they enter the password rather then having to push a login button. I have looked all over the net for a way to do this and have not found one yet. Any help would be great however I am very new to vb.net so please show complete code if you have the answer or I will get lost. :-)

Also, I will need to do a if statement in there also to validate the password.

Thank you!!! :D
 
no need to trap the keys
use DialogResult property of your buttons and form as well as Form.AcceptButton and Form.CancelButton

research DialogResult and Modal Forms

heres the gist -
(assumptions
form class is called LogonForm,
LogonForm contains text boxes, txtUser, txtPW
LogonForm has function Logon(userName, password) as boolean in its scope.

drop two buttons on the form, btnOk and btnCancel
set the DialogResult on the btnOk button to DialogResult.Ok
set the DialogResult on the btnCancel button to DialogResult.Cancel

set LogonForm.AcceptButton property to the btnOk button
set LogonForm.CancelButton property to the btnCancel button

in the closing event:
Code:
Private Sub Form1_Closing(ByVal sender As Object, _ 
		ByVal e As System.ComponentModel.CancelEventArgs) _ 
		 Handles MyBase.Closing
	if me.DialogResult = DialogResult.Ok then
		if not Logon(txtUser, txtPW)
			 MessageBox.Show("Logon Failed")
			 e.Cancel = true
		end if
	end if
End Sub

open the form like this -

Code:
with new LogonForm
	if .ShowModal() = DialogResult.Cancel then
		MessageBox.Show("User Cancelled")
		End
	.Dispose()
end with
' Continue with the rest of your program here
 
Joe Mamma said:
Code:
with new LogonForm
	if .ShowModal() = DialogResult.Cancel then
		MessageBox.Show("User Cancelled")
		End
	.Dispose()
end with
' Continue with the rest of your program here
should have read

Code:
with new LogonForm
	if .ShowDialog() = DialogResult.Cancel then
		MessageBox.Show("User Cancelled")
		End
	.Dispose()
end with
' Continue with the rest of your program here

My Delphi crept in there!!!
 
Back
Top