Search code examples
c#winformscontrolskeystroke

ProcessCmdKey does not pass Enter Key


Something has happened that is causing my code to always capture the Enter key but never process it. Meaning all my data entry boxes, grid controls etc. are broken. It is like the Enter key has been switched off.

I have tried removing the ProcessCmdKey routine completely and it is still doing it.

I had a rather lengthy piece of code that processes the enter key on a tabbed form, but it was messing up the other tabs, meaning that whenever enter was pressed it would capture it.

So, I just checked to see what tab it was on and ignore it if not the correct tab, simple? no.

For whatever reason no matter what I do my form will NOT process the enter key.

I have gone back to the most basic of code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    return base.ProcessCmdKey(ref msg, keyData);
}

From what I understand the above should really do nothing, just pass the key pressed back and then process it.

And that works for everything except the enter key. If I have a textbox or any control then it will take all input, but it just completely ignores the enterkey. I have tried return false; as well and that does not work.

I have even tried completely removing the routine and it is still broken. restarted VS just in case.

EDIT: OK, here is the complete original code before I started to "fix" things. This works correctly, except it stops the Enter key from working anywhere else in the program. Which as you can imagine is not greatly appreciated by the end users.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {

        if (ActiveControl.Name.ToString() != "order_creation_grid") //ensures that we don't caputre keypresses in the datagrid
        {

            //if (keyData == (Keys.Tab | Keys.Shift)) //tabbing backwards
            //{
            //    if (ActiveControl.Parent.Name.ToString() == "order_con_name_DD")
            //    {
            //        order_con_name_DD.Select();
            //        order_con_name_DD.ShowDropDown();

            //    }
            //}
            if (keyData == Keys.Tab || keyData == Keys.Enter)
            {
                if (ActiveControl.Parent.Name.ToString() == "order_shipper_dd")
                {
                    var test = keyData;
                    //getAllConsignees(); remove
                    get_all_addresses(); //load up the address dd
                    order_address_dd.SelectedIndex = 0;
                    order_address_dd.Select();
                    order_address_dd.Focus();
                    return true;//lets the system know that the control key was handled and not to fire it again
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_address_dd")
                {
                    if (order_address_dd.SelectedIndex != -1)//only do this if there is not a new address in the address chooser ie: found one in database
                    {
                    order_match_dd.Select();
                    order_match_dd.Focus();
                        order_match_dd.ShowDropDown();
                    return true;

                    }
                    else
                    {
                        order_address_eb.Value = order_address_dd.Text;
                        order_consignee_eb.Select();
                        order_consignee_eb.Focus();
                        return true; ;
                    }
                }
                else if (ActiveControl.Name.ToString() == "order_match_dd")
                {
                    if (order_match_dd.SelectedIndex != -1)//fire off when a valid selection is made
                    {
                        //check if it is a new item

                        if (order_match_dd.Text == "1 New")
                        {
                            //blank out the fields and position cursor to Consignee
                            clear_order_fields();

                            order_consignee_eb.Select();
                            order_consignee_eb.Focus();
                        }
                        else //parse the value in the match box useing the seperator | into strings and load up the correct fields
                        {
                            char[] delimiterChars = { '|' };
                            string[] fields = order_match_dd.Text.Split(delimiterChars);

                            clear_order_fields();

                            order_consignee_eb.Value = fields[0].ToString().Trim(); ;
                            order_address_eb.Value = fields[1].ToString().Trim();
                            order_city_eb.Value = fields[2].ToString().Trim();
                            order_state_eb.Value = fields[3].ToString().Trim();
                            order_zip_eb.Value = fields[4].ToString().Trim();
                            //go try and match the driver and facility with this zipcode
                            get_driver_facility();

                            order_BOL_eb.Select();
                            order_BOL_eb.Focus();
                        }
                        return true;
                    }

                }
                else if (ActiveControl.Parent.Name.ToString() == "order_BOL_eb")
                {
                    int custID = Convert.ToInt16(order_shipper_dd.SelectedValue);

                    string testFor = order_BOL_eb.Value.ToString();

                    var lookFor = (from l in dbContext.stop_details
                                   where l.cust_unique_id == testFor && l.customer_id == custID
                                   select l).ToArray();

                    int count = lookFor.Count();
                    if (count > 0)
                    {
                       MessageBox.Show("WARNING..this BOL has been used before for this customer, make sure you really want to use it again.");
                    }
                    order_loose_qty.Focus();
                    return true;
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_consignee_eb")
                {

                    order_address_eb.Select();
                    order_address_eb.Focus();
                    return true;
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_address_eb")
                {

                    order_city_eb.Select();
                    order_city_eb.Focus();
                    return true;
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_city_eb")
                {

                    order_state_eb.Select();
                    order_state_eb.Focus();
                    return true;
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_state_eb")
                {

                    order_zip_eb.Select();
                    order_zip_eb.Focus();
                    return true;
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_zip_eb")
                {
                  //  get_driver_facility();

                    order_BOL_eb.Select();
                    order_BOL_eb.Focus();
                    return true;
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_note")
                {
                   //auto send to the grid if checkboxed

                    if (autoSend_cb.Checked)
                    {
                        send_to_orderGrid();

                    }
                    //otherwise just sit there...
                    return true;
                }
            }


            if (keyData == Keys.Enter) //On enter key for these controls fire the tab key so that it will move to the next control
            {

                if (ActiveControl.Parent.Name.ToString() == "order_loose_weight")
                {
                    SendKeys.Send("{TAB}");
                }
                else if (ActiveControl.Parent.Name.ToString() == "order_loose_qty")
                {

                    SendKeys.Send("{TAB}");

                }
                else if (ActiveControl.Parent.Name.ToString() == "order_pallets_qty")
                {

                    SendKeys.Send("{TAB}");

                }
                else if (ActiveControl.Parent.Name.ToString() == "order_pallets_weight")
                {

                    SendKeys.Send("{TAB}");

                }
                else if (ActiveControl.Parent.Name.ToString() == "order_hazmat_weight")
                {

                    SendKeys.Send("{TAB}");

                }
                else if (ActiveControl.Parent.Name.ToString() == "order_COD")
                {

                    SendKeys.Send("{TAB}");

                }

            }
            if (keyData == (Keys.Control | Keys.OemCloseBrackets))
            {
                this.order_shipper_dd.Select();
                this.order_shipper_dd.ShowDropDown();
                return true;
            }

            if (keyData == (Keys.Control | Keys.Enter)) //when ctrl-enter is pressed in the Order Entry screen
            {
                send_to_orderGrid();


                return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

I can even comment out the entire routine and it still does it.


Solution

  • I am unsure as to what you need. You have not defined what "process" the enter key means. My best guess:

    1- Do you want the enter key to perform a new line in your text boxes? Change the AcceptsReturn property from False to True, and your text box should allow you to use the enter key to create a new line.

    2 - Use the enter key to tab to the next control? Add a keyDown event to a text box and do something like this:

    switch (e.KeyCode)
    {
        case Keys.Enter:
            SendKeys.Send("{TAB}");
            break;
    }