C#KeyDown问题

时间:2018-07-17 08:33:02

标签: c# winforms events keydown

我正在尝试制作一个包含三个按钮的菜单(依次为:播放,选项,退出),其中只有选中的按钮带有边框,并由向上和向下箭头控制。不幸的是,当按下atm按钮时似乎什么也没发生。这是代码:

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;
            }
        }
    }

祝你有美好的一天:)

1 个答案:

答案 0 :(得分:0)

您的代码中有两个问题。第一个在前两个if语句中。您正在正确地更改索引,但没有将边框设置为新的选定按钮。您必须删除以前选择的按钮的边框并设置新选择的按钮的边框。

第二个是您忘记为按钮设置单击事件,因此单击按钮不会执行任何操作。这是您的代码应为的方式:

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:请注意不必要的代码,例如selectedButton变量和构造函数中的if语句。它们不会影响功能,但可能会阻止以后的代码维护。