if(e.KeyChar ==(char)ConsoleKey.F1)不响应C#.Net Visual Studio

时间:2015-11-10 00:28:35

标签: c# visual-studio key-bindings

HelloOverflow的其他用户

所以我正在开展一个有趣的小项目&我非常接近完成,我遇到的唯一问题是让键绑定到按钮。 我可能太接近这个问题,因为我目前正在同时处理两个项目,一个是网络浏览器和一个关键绑定在那里工作得很好但是当我尝试使用相同的概念它不想回应时,我是否错过了什么?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
/// <summary>
/// Version: 1.0
/// Farm SoundBoard
/// Created By Me
/// Date 2015-10-29
/// Category: Fun
/// </summary>
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.cow;
            player.Play();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.bird;
            player.Play();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.bee;
            player.Play();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.elephant;
            player.Play();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.tiger;
            player.Play();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.cat;
            player.Play();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.dog;
            player.Play();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.lion;
            player.Play();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.flies;
            player.Play();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.fish;
            player.Play();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.parrot;
            player.Play();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.car;
            player.Play();
        }
        private void button14_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)ConsoleKey.F1)

                    {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                player.Stream = Properties.Resources.cow;
                player.Play();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您使用了错误的比较,ConsoleKey通常用于Console.ReadKey。请改为使用Keys枚举。