CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    82

    [RESOLVED] KeyPress, KeyCode handling behavior strange

    Hi all,
    I basically want to have a combo that lists some account codes (e.g ALB1E, BSE2F) and when the user types the first few letters and presses enter, I calla function that will get all matching acocunt codes (e.g when they type ALB, it will show all ALB%) and populate them in a combo.

    The problem is when I handle e.keycode == Enter, it gets called TWICE and the resulting is a wrong search results.

    Code:
    private void cmbAccountCodes_KeyDown(object sender, KeyEventArgs e)
            {
    if (e.KeyCode == Keys.Enter)
    {
         GetDeliveryPoints(cmbAccountCodes.Text.Trim()); //gets the data I want to populate and adds it to the combo as well 
    }
    }
    So, when I press ALB and press Enter, it egts matching results like ALB10Q, ALB67Y, ALB40W etc and populates them in a combo. But then, this method gets called again somehow, and this time the first item in the combo is selected ( int his case its ALB10Q) and then it searches again for ALB10Q% passing it as param to GetDeliveryPoints() which I do not want. Why is the KeyDown method called twice? Thanks for the help.

  2. #2
    Join Date
    Jul 2010
    Posts
    82

    Re: KeyPress, KeyCode handling behavior strange

    Ok, I fixed this error a min after I posted it -- but its still strange.

    So, GetDeliveryPoints() had this statement: cmbAccountCodes.DroppedDown = true;

    This was causing the keydown event somehow to be called twice. I commented this and now it works fine!! Any inputs would still be a learning though on this.

  3. #3
    Join Date
    Dec 2009
    Posts
    596

    Re: KeyPress, KeyCode handling behavior strange

    In vb I would have done it through the form's keyup.

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: KeyPress, KeyCode handling behavior strange

    What I just said wasn't true. Disregard that unless you like pain.

  5. #5
    Join Date
    Jul 2010
    Posts
    82

    Re: KeyPress, KeyCode handling behavior strange

    Quote Originally Posted by viperbyte View Post
    What I just said wasn't true. Disregard that unless you like pain.

    Thanks for the inputs though. As I said, I fixed it by commenting out the droppeddown statement. I understand it as when a combobox drops down it generates drop down event which also is an equivalent of Enter or a combo of LButton + MBUtton and Back or something like that , hence a combo dropdown = Enter key value. So, any code written for handling Enter in a combo, will also be invoked when it drops down.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured