简体   繁体   English

C#KeyDown问题

[英]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. 不幸的是,当按下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. 第一个在前两个if语句中。 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. PS:请注意不必要的代码,例如selectedButton变量和构造函数中的if语句。 They do not affect the functionality, but may hinder later code maintenance. 它们不会影响功能,但可能会妨碍以后的代码维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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