0

So I finally figured out how to bind the button I wanted to bind on my soundboard but now the code gives me an error.

Severity Code Description Project File Line
Warning CS0642 Possible mistaken empty statement WindowsFormsApplication6 C:\Users\User\Documents\Visual Studio 2015\Projects\WindowsFormsApplication6\Form1.cs 125

I feel like I'm missing something but I really cant see what I have missed. I tried a few different solutions but it didnt do me any good, I tried some trial and error and that just made me end up back here.

    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 Form1_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();
        }
    }
 }
  • `if (e.KeyChar == (char)ConsoleKey.F1) ;` See the semi-colon at the end? That means that the `if` statement ends there. Its saying that you aren't doing anything with the `if`. If its supposed to run the 3 lines under it, you need to wrap them in brackets just like you do with methods. – Ron Beyer Nov 10 '15 at 02:37
  • That worked! I cant belive I didnt see that! One small issue left, it doesnt respond to the keybind. (F1) – Haley Alisson Nov 10 '15 at 02:39
  • Try setting the form property KeyPreview to true, otherwise whatever control is active will get the key press first and probably swallow it before it gets to the form level. – Ron Beyer Nov 10 '15 at 02:42
  • That didnt do the trick, I will take haneys advice & google it & if I get stuck open another question about this subject, thanks for the help! – Haley Alisson Nov 10 '15 at 02:45
  • Another trick: Use the `KeyUp` event instead, it has a `KeyCode` property that directly maps to keys like you want. For example, in the `KeyUp` event, your if statement would look like this: `if (e.KeyCode == Keys.F1)`, see this: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup(v=vs.110).aspx I think the problem you have is that you are trying to convert a ConsoleKey to a char, and F1 is not a printable character (and doesn't have a char code). – Ron Beyer Nov 10 '15 at 02:48
  • Thanks! I will try that out! – Haley Alisson Nov 10 '15 at 02:49
  • I think I will have to use that because when I tried to simulate a mouse press it didnt work either if (e.KeyChar == (char)ConsoleKey.F1) { button1_Click(null, null); – Haley Alisson Nov 10 '15 at 02:49
  • 1
    Sorry for inundating you, but here is another solution: http://stackoverflow.com/questions/17948204/keypress-f1-does-not-work-c-sharp `if (e.KeyCode.ToString() == "F1")` and still use the KeyPress event. – Ron Beyer Nov 10 '15 at 02:51

2 Answers2

1

The problem is this line:

if (e.KeyChar == (char)ConsoleKey.F1) ;

An if statement encapsulates some code to execute only if the condition is true. If statements in C# typically encapsulate their body of conditional code in { } brackets. As a courtesy, the compiler assumes that a lack of brackets means that there's one line in the conditional body: the next line of code you write. In your case, you've basically written:

if (e.KeyChar == (char)ConsoleKey.F1) { }

Because the ; at the end of the if statement terminates the line of code. What you might do is this:

 if (e.KeyChar == (char)ConsoleKey.F1) /* some code here */;

Or this:

if (e.KeyChar == (char)ConsoleKey.F1)
{
    // Some code here
}
Haney
  • 32,775
  • 8
  • 59
  • 68
  • That worked! So that issue is solved! Now I just need to get the F1 to respond and actually do something, because for the time being it doesnt play the sound when the keybind (F1) is being pressed. – Haley Alisson Nov 10 '15 at 02:41
  • @HaleyAlisson experiment, Google (and hopefully find the answer on our site!). If you get stuck with that, ask a second question on that topic. :) – Haney Nov 10 '15 at 02:42
0

So this is what happend.

I tried to allow the KeyPreview & KeyDown in the form properties & it didnt work but as soon as I tried adding the same function with code it worked.

Here is what worked

private void Form1_Load(object sender, EventArgs e)
{
    this.KeyPreview = true;
    this.KeyDown += new KeyEventHandler(button1_KeyDown);
}

And

private void button1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode.ToString() == "F1")

    {
        button2_Click(null, null);
    }

So I still had to simulate the button being pressed by the mouse but hey it worked!