Domanda

So I had to override the ProcessCmdKey in order to detect tab pressing in my winform. See this question for context. Now that I was successful in fixing the tabbing issue I had I now realize that I need to also check for shift+tab logic to allow the user to go backwards. I can't seem to figure it out. Below is some of what I have tried and it hasn't worked thus far.

    private bool isTab = false;
    private bool isShiftTab = false;
    private  StringBuilder ShiftTab = new StringBuilder();

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

        if (keyData == Keys.Tab)
        {
            isTab = true;
            ShiftTab.Append("Tab");
        }
        else
        {
            isTab = false;
        }

        if (keyData == Keys.Shift)
        {
            ShiftTab.Append("Shift");
        }

        if (ShiftTab.ToString() == "TabShift" || ShiftTab.ToString() == "ShiftTab")
        {
            isShiftTab = true;

        }

        if ((Control.ModifierKeys & Keys.Tab) != 0)
        {
           //code
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
È stato utile?

Soluzione

I think you have to combine the two keys like so:

if (keyData == (Keys.Shift | Keys.Tab)) isShiftTab = true;

Which you can then use to skip your tab override.

Altri suggerimenti

You can combine values of Keys using logical or |

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.LButton | Keys.Shift | Keys.Tab))
        {
            // your code to handle tab shift
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top