Question

I am trying to process to keydown event but the below code is process only first char of string and not able to remove second char of string.

private void Game_KeyDown(object sender, KeyEventArgs e)
{
    if(BirdsArray[SelectedIndex].THIS_STRING.First()== (char)e.KeyData)
    {
        BirdsArray[SelectedIndex].THIS_STRING = BirdsArray[SelectedIndex].THIS_STRING.Remove(0, 1);
        this.Validate();
    }
}

In this below code BirdsArray is array of panels, THIS_STRING is a string type of string object in BirdsArray.

Était-ce utile?

La solution

The KeyDown event gives you the virtual key code of the key that was pressed. Like Keys.A if you press the A-key. It just tells you about the key, not about the letter that's produced by the key. Which depends on the active keyboard layout (it is not an A in Japan or Russia for example) and what modifier keys are down. Shift being the obvious example, producing either A or a. Or Alt which, when down, doesn't produce a letter at all.

So your code will work by accident if the first letter of the string is capitalized. You'll run out of luck when the rest are not. A does not match a.

It is pretty unclear to me why you need this feature. Do consider the KeyPress event instead. Which gives you the actual letter that the user sees on the screen.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top