0

Hello fellow users of StackOverflow

So I am working on a fun little project & I am very close to being done, the only problem I have been running into is getting the keys to bind to the buttons. I might be too close to the issue since I am currently working on two projects at the same time, one web browser & the key binds works perfectly there but when I try to use the same concept it doesnt want to respond, Did I miss something?

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();
            }
        }
    }
}
  • 2
    Tip: name your buttons. `button12` means nothing. – Dai Nov 10 '15 at 00:30
  • I think I know what you mean, it would be easier to keep track of which one is which, thank you for the tip! WIll be used! – Haley Alisson Nov 10 '15 at 00:31
  • ^names should be meaningful....imagine if you had 1000s of other lines of code wrapped around this and another Jr. Developer came in and modified your code and had figure out which buttons are which....properly named buttons and other objects makes code more readable and easy to maintain. – J.S. Orris Nov 10 '15 at 00:34
  • Yeah I figured that out after a bit of thinking! Thanks for the tip, will be used! – Haley Alisson Nov 10 '15 at 00:35

1 Answers1

1

I think you're using the wrong comparison, ConsoleKey is usually used for Console.ReadKey. Try the Keys enumeration instead.

Dan Chase
  • 993
  • 7
  • 18