Question

Ok, I want the user to be able to press enter to initiate a click button during textbox entry.

I have the following code:

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
            if (e.KeyValue == 13)
            {
                button3_Click(sender, e);
            }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
    }
    private void button3_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("Please enter a value.", "No name entered", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else
        {
            if (listBox1.Items.Contains(textBox1.Text) == true)
            {
                MessageBox.Show("You have tried to enter a duplicate.", "No duplicates allowed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                listBox1.Items.Add(textBox1.Text);
                textBox1.Text = "";
            }
        }


    }

However, when I press enter the value saves and then the MessageBox comes up saying "please enter a value" about 4 times. How can I make this code make the button_click only happen once on pressing enter?

Is there an easier way of doing this?

Thank you!

Was it helpful?

Solution

//Create a new button
//Assuming you have a new button named "acceptButton"
//Assuming your form name is "FormName"
FormName.AcceptButton = acceptButton;
//this button automatically is triggered at enter key is pressed
acceptButton += new Click(acceptButton_Click);

void acceptButton_Click(object sender, EventArgs e) {
     button3_Click(sender, e);
}

or

//Make button3 the one to be activated when enter key is pressed
FormName.AcceptButton = button3;
//so that when enter key is pressed, button3 is automatically fired

OTHER TIPS

First, I know this is a very old post, secondly why could you simply just use the KeyUp event of the Textbox and just call the Button's Click event:

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyValue == 13)
    {
        this.Accept_Click(null, null);
    }
}

Unless I am missing something which is quite possible ;-)

Gian was right, thank you. the final code is:

        private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
    }

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 13)
        {
            AcceptButton = button3;
        }
    }

I don't want to make you feel bad, but what were you trying to do in the textbox_textChanged method?

First thing you want to do is remove it. What it does is add the button3_Click to the KeyUp event. Every time the text changes it adds it again, and the button3_Click method will be called many times.

What you get is probably a "You have tried to enter a duplicate" message. This is because the button3_Click method is called more then once with the same value (first time the value is added, and on the following calls it tries to add that same value again).

In any case, try to add information to your question, it's very unclear(!) and takes a while to understand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top