Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyUp Event keeps firing events how to limit

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello,

New to C#, I have been trying to figure out why the keyUP event I coded continues to trigger and how to suppress it.

KeyUp code that I am using is shown below. I have tried handled, suppressKeyPress, but neither keep it from firing once it performs the performClick action.

What I am trying to do is:

if the user hits Enter when they are in the textbox, it should have the same effect as if they clicked the addrFormGenerateButton (which is why i am sending it to addrFormGenerateButton.PerformClick()).

Not sure if this is the correct way to do this, if it is, then how do I keep all subsequent enters from firing.

Currently, if I hit the Enter button and generate and error due to invalid data in the textbox, when I click Enter to accept the messageBox this event fires again and again.

Thanks for all the help in advanced.

Code:
        private void addrFormTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                addrFormGenerateButton.PerformClick();
            }
        }

private void addrFormGenerateButton_Click(object sender, EventArgs e)
        {
            string validPathRequiredErrorString = "Please enter a valid address before attempting to process.";
            string validPathRequiredErrorBoxString = "Valid path required!";
            this.addrFormTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.addrFormTextBox_KeyUp);
            if (this.addrFormTextBox.Text.Length == 0 || this.addrFormTextBox.Text == null)
            {
                MessageBox.Show(validPathRequiredErrorString, validPathRequiredErrorBoxString);
                return;
            }
 
you are assigning an event handler to the key up event every time you click the button.

so the first time you click the button you assign addrFormTextBox_KeyUp to the textbox once.
then then you press KeyUp, and if it's enter you will assign the handler addrFormTextBox_KeyUp a second time. the next time you press enter in the textbox the click will preform twice, assigning the key up 2 more times. and the pattern continues.

you can assign the KeyUp event to the textbox when the form is initialized (along with the other form events). and remove it from click event. this assumes the textbox is present at design time. if you dynamically add the textbox to the form at runtime, then the code is slightly different.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top