getting key pressed

J

Jerry

Hello,

I have put a simple form together with a lable and a textbox. I want to be
able to trap the ENTER key when is pressed. So I can check the texted
enetered. If correct, call up the main program, if not re-prompt for the
user ID.

Thanks,

Jerry
 
J

Jerry

Added code:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace passGL

{

public partial class UserLogin : Form

{

public UserLogin()

{

InitializeComponent();

}

private void label1_Click(object sender, EventArgs e)

{

}

private void UserLogin_Load(object sender, EventArgs e)

{

}

private void txtBxUserID_TextChanged(object sender, EventArgs e)

{

// if (e.KeyChar == Keys.M) e.Handled = true;



// if (e.Equals( Keys.M ) )

// {

// Console.WriteLine("Enter pressed!!");

// }

}

}

}
 
M

Morten Wennevik [C# MVP]

Hi Jerry,

I wonder if you are trying to mimick the functionality of an
AcceptButton. Form.AcceptButton will fire the defined button event on an
Enter key anywhere on the Form, likewise the Form.CancelButton will trap
Escape. In the button event find out if the text entry is valid.

If you really want to capture the Enter key inside the TextBox, try using
the KeyPress event

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
MessageBox.Show("Enter");
}

or the KeyDown or KeyUp event

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
MessageBox.Show("Enter");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top