所以我终于想出了如何绑定我想在我的音板上绑定的按钮,但现在代码给了我一个错误。
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
我觉得我错过了什么,但我真的无法看到我错过了什么。 我尝试了一些不同的解决方案,但它对我没有任何好处,我尝试了一些试验和错误,这让我最终回到了这里。
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();
}
}
}
答案 0 :(得分:1)
问题在于这一行:
if (e.KeyChar == (char)ConsoleKey.F1) ;
如果条件为if
,true
语句将封装一些代码以仅执行。如果C#中的语句通常在{ }
括号中封装它们的条件代码体。作为礼貌,编译器假定缺少括号意味着条件体中有一行:您编写的下一行代码。在你的情况下,你基本上写了:
if (e.KeyChar == (char)ConsoleKey.F1) { }
因为if语句末尾的;
终止了代码行。你可能会这样做:
if (e.KeyChar == (char)ConsoleKey.F1) /* some code here */;
或者这个:
if (e.KeyChar == (char)ConsoleKey.F1)
{
// Some code here
}
答案 1 :(得分:0)
所以这就是发生的事情。
我试图允许KeyPreview&amp;表单属性中的KeyDown&amp;它没有用,但只要我尝试使用相同的功能添加相同的功能就可以了。
这是有用的
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(button1_KeyDown);
}
并且
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F1")
{
button2_Click(null, null);
}
所以我仍然需要模拟鼠标按下的按钮,但是它有效!