Click here to Skip to main content
15,885,757 members
Home / Discussions / C#
   

C#

 
AnswerRe: Detect Enter key press - click button when pressed Pin
Dave Kreskowiak26-Dec-14 16:07
mveDave Kreskowiak26-Dec-14 16:07 
GeneralRe: Detect Enter key press - click button when pressed Pin
BillWoodruff26-Dec-14 23:04
professionalBillWoodruff26-Dec-14 23:04 
GeneralRe: Detect Enter key press - click button when pressed Pin
Dave Kreskowiak27-Dec-14 5:13
mveDave Kreskowiak27-Dec-14 5:13 
AnswerRe: Detect Enter key press - click button when pressed Pin
tarun199126-Dec-14 19:16
professionaltarun199126-Dec-14 19:16 
GeneralRe: Detect Enter key press - click button when pressed Pin
robwm127-Dec-14 6:24
robwm127-Dec-14 6:24 
GeneralRe: Detect Enter key press - click button when pressed Pin
tarun199128-Dec-14 7:45
professionaltarun199128-Dec-14 7:45 
GeneralRe: Detect Enter key press - click button when pressed Pin
tarun199131-Dec-14 7:42
professionaltarun199131-Dec-14 7:42 
AnswerRe: Detect Enter key press - click button when pressed Pin
BillWoodruff26-Dec-14 20:02
professionalBillWoodruff26-Dec-14 20:02 
In addition to the method that Dave shows, you can also set the Form's KeyPreview property to 'true, and catch the Enter Key in a KeyUp EventHandler:
C#
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Console.WriteLine("enter key seen");
    }
}
If you have both an 'AcceptButton defined for the Form, and KeyPreview set to 'true, and a KeyUp EventHandler: both Events will fire, with the Button getting a "virtual click" first.

However, I think using either of these two techniques is (almost always) a mistake because most applications are going to need to perform some kind of validation on user entry before "submit."

With either of these two techniques, you won't know where the Enter key Event came from (okay, there is a work-around for that using 'ActiveControl, but I'll skip over that).

imho a much better technique is something like this:
C#
private void button1_Click(object sender, EventArgs e)
{
    Console.WriteLine("button click ... active Control is {0}", ActiveControl.Name);

    // submit code goes here
}

private void TextBoxes_KeyUp(object sender, KeyEventArgs e)
{
    if (! (e.KeyCode == Keys.Enter)) return;

    // disable submit ?
    button1.Enabled = false;

    TextBox activeTextBox = sender as TextBox;

    string currentText = activeTextBox.Text;
    
    // perform Validation here for conditions which must be true
    // for all TextBoxes that use this EventHandler

    // no TextBox can have more than 3 digits
    if(currentText.Where(ch => Char.IsDigit(ch)).ToList().Count > 3)
    {
        // for testing only
        Console.WriteLine("fail: TextBox{0} has more than 3 digits", activeTextBox);
        
        activeTextBox.Focus();
        e.Handled = true;
        return;
    }

    // perform Validation here that is unique to each TextBox

    switch (activeTextBox.Name)
    {
        case "textBox1":
            // example of unique validation
            // textBox1 must have at least 4 alpha-characters
            if (! (currentText.Where(ch => Char.IsLetter(ch)).ToList().Count == 4))
            {
                // for testing only
                Console.WriteLine("fail: TextBox1 does not have 4 letters");
                
                activeTextBox.Focus();
                e.Handled = true;
                return;
            }
            break;
        case "textBox2":
            break;
        case "textBox3":
            break;
    }

    // okay ... submit
    button1.Enabled = true;
    button1.PerformClick();
}
Note that you need to wire-up the KeyUp EventHandler to each of the TextBoxes you wish to validate.

Also, note that the decision to disable the Button used for "submit" reflects my own conviction that it is better to have UI controls that do not function if using them would have unknown side-effects, or allow errors.

While disabling the Button may not quite make sense in this example; my choice would be to enable the Button only when all 3 TextBoxes have validated content. Your Application design may not require this.

Writing code to keep track of which TextBoxes are currently validated, and to evaluate whether all TextBoxes have valid content is not difficult.

Validation is a good thing !
«OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. »  Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."

GeneralRe: Detect Enter key press - click button when pressed Pin
robwm127-Dec-14 6:22
robwm127-Dec-14 6:22 
QuestionIn Custom Featured MessageBox how to change font size Pin
Member 1111316725-Dec-14 18:47
Member 1111316725-Dec-14 18:47 
AnswerRe: In Custom Featured MessageBox how to change font size Pin
BillWoodruff25-Dec-14 18:57
professionalBillWoodruff25-Dec-14 18:57 
AnswerRe: In Custom Featured MessageBox how to change font size Pin
syed shanu25-Dec-14 19:56
mvasyed shanu25-Dec-14 19:56 
AnswerRe: In Custom Featured MessageBox how to change font size Pin
deepankarbhatnagar26-Dec-14 1:46
professionaldeepankarbhatnagar26-Dec-14 1:46 
AnswerRe: In Custom Featured MessageBox how to change font size Pin
OriginalGriff26-Dec-14 2:46
mveOriginalGriff26-Dec-14 2:46 
Questionloop through user controls Pin
Jassim Rahma24-Dec-14 4:32
Jassim Rahma24-Dec-14 4:32 
AnswerRe: loop through user controls Pin
OriginalGriff24-Dec-14 4:46
mveOriginalGriff24-Dec-14 4:46 
GeneralMessage Closed Pin
24-Dec-14 7:07
professionalBillWoodruff24-Dec-14 7:07 
GeneralRe: loop through user controls Pin
OriginalGriff24-Dec-14 7:13
mveOriginalGriff24-Dec-14 7:13 
GeneralRe: loop through user controls Pin
Jassim Rahma24-Dec-14 19:31
Jassim Rahma24-Dec-14 19:31 
AnswerRe: loop through user controls Pin
Kornfeld Eliyahu Peter24-Dec-14 19:48
professionalKornfeld Eliyahu Peter24-Dec-14 19:48 
GeneralRe: loop through user controls Pin
OriginalGriff24-Dec-14 20:05
mveOriginalGriff24-Dec-14 20:05 
QuestionBest way to generate bulk pdf reports from DB. Pin
Member 1133296924-Dec-14 2:06
Member 1133296924-Dec-14 2:06 
SuggestionRe: Best way to generate bulk pdf reports from DB. Pin
Richard MacCutchan24-Dec-14 2:16
mveRichard MacCutchan24-Dec-14 2:16 
GeneralRe: Best way to generate bulk pdf reports from DB. Pin
Member 1133296924-Dec-14 2:53
Member 1133296924-Dec-14 2:53 
AnswerRe: Best way to generate bulk pdf reports from DB. Pin
OriginalGriff24-Dec-14 3:07
mveOriginalGriff24-Dec-14 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.