简体   繁体   中英

C# KeyDown Issue

I am trying to make a menu with three buttons (in order: Play, Options, Exit) in which only selected button has a border and that is controlled with arrows UP and DOWN. Unfortunately nothing seems to be happening when buttons are pressed atm. Here's the code:

public partial class
{
    int i = 0;
    List<Button> menuButtons = new List<Button>();
    Button selectedButton = new Button();

    public Menu()
    {
        InitializeComponent();

        menuButtons.Add(btnPlay);
        menuButtons.Add(btnOptions);
        menuButtons.Add(btnExit);

        selectedButton = menuButtons[i];

        if (menuButtons[i] == selectedButton)
        {
            menuButtons[i].FlatAppearance.BorderSize = 1;
        }
    }

    private void Menu_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Down)
        {
            if (i < menuButtons.Count)
            {
                i++;
            }
            else if (i >= menuButtons.Count)
            {
                i = 0;
            }
        }

        if (e.KeyCode == Keys.Up)
        {
            if (i > 0)
            {
                i--;
            }
            else if (i <= 0)
            {
                i = menuButtons.Count;
            }
        }

        if (e.KeyCode == Keys.Enter)
        {
            switch (i)
            {
                case 0:
                    btnPlay.PerformClick();
                    break;
                case 1:
                    btnOptions.PerformClick();
                    break;
                case 2:
                    btnExit.PerformClick();
                    break;
            }
        }
    }

Have a nice day :)

There are two problems in your code. The first one is in the first two if statements. You are correctly changing the index, but you are not setting the border to the new selected button. You must remove the border of the previously selected button and set the new selected button's border.

The second is that you forgot to set click events for your buttons, so they'll do nothing when clicked. Here's how your code should be:

public partial class
{
    int i = 0;
    List<Button> menuButtons = new List<Button>();
    Button selectedButton = new Button();

    public Menu()
    {
        InitializeComponent();

        //Assigning click events for the buttons.
        btnPlay.Click += BtnPlay_Click;
        btnOptions.Click += BtnOptions_Click;
        btnExit.Click += BtnExit_Click;

        menuButtons.Add(btnPlay);
        menuButtons.Add(btnOptions);
        menuButtons.Add(btnExit);

        selectedButton = menuButtons[i];

        if (menuButtons[i] == selectedButton)
        {
            menuButtons[i].FlatAppearance.BorderSize = 1;
        }
    }

    private void Menu_KeyDown(object sender, KeyEventArgs e)
    {
        //Removing border from previously selected button.
        menuButtons[i].FlatAppearance.BorderSize = 0; 

        if (e.KeyCode == Keys.Down)
        {
            if (i < menuButtons.Count)
            {
                i++;
            }
            else if (i >= menuButtons.Count)
            {
                i = 0;
            }
        }

        if (e.KeyCode == Keys.Up)
        {
            if (i > 0)
            {
                i--;
            }
            else if (i <= 0)
            {
                i = menuButtons.Count;
            }
        }

        //Setting border for the newly selected button.
        menuButtons[i].FlatAppearance.BorderSize = 1;

        if (e.KeyCode == Keys.Enter)
        {
            switch (i)
            {
                case 0:
                    btnPlay.PerformClick();
                    break;
                case 1:
                    btnOptions.PerformClick();
                    break;
                case 2:
                    btnExit.PerformClick();
                    break;
            }
        }
    }

    private void BtnExit_Click(object sender, EventArgs e)
    {
        //Code for the Exit button.
    }

    private void BtnOptions_Click(object sender, EventArgs e)
    {
        //Code for the Options button.
    }

    private void BtnPlay_Click(object sender, EventArgs e)
    {
        //Code for the Play button.
    }
}

PS: pay attention to unnecessary code like the the selectedButton variable and the if statement in the constructor. They do not affect the functionality, but may hinder later code maintenance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM