计时器事件c#崩溃
我有一个计时器事件,如下所示,我从帖子中得到了一些建议。你能告诉我这个有什么问题吗?我遇到了以下崩溃: 无法将类型为计时器事件c#崩溃,c#,winforms,timer,crash,C#,Winforms,Timer,Crash,我有一个计时器事件,如下所示,我从帖子中得到了一些建议。你能告诉我这个有什么问题吗?我遇到了以下崩溃: 无法将类型为System.Windows.Forms.Timer的对象强制转换为类型System.Windows.Forms.Button。 对我的错误有什么建议吗 public MainForm() { InitializeComponent(); ButtonTimer.Tick += new EventHandler(ButtonTimer_Tick); But
System.Windows.Forms.Timer
的对象强制转换为类型System.Windows.Forms.Button
。
对我的错误有什么建议吗
public MainForm()
{
InitializeComponent();
ButtonTimer.Tick += new EventHandler(ButtonTimer_Tick);
ButtonTimer.Interval = 100;
}
private void ButtonTimer_Tick(object sender, EventArgs e)
{
Button CurrentButton = (Button)sender;
string PressedButton = CurrentButton.Name;
switch (PressedButton)
{
case "BoomUp":break;
}
}
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
private void BoomUp_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ButtonTimer.Stop();
}
}
在
buttonimer\u Tick
中,发送者是计时器,而不是按钮。因此,您将计时器投射到按钮中(该方法的第1行)。在按钮提示中,发送者是计时器,而不是按钮。因此,您将计时器投射到按钮中(该方法的第一行)。您在按钮时间\u勾选方法中遇到问题:
Button CurrentButton = (Button)sender;
发送方
不是按钮
,而是计时器
那你现在要做什么
您可以在类中添加一个私有字段
private Button currentButton_;
及
及
您在按钮时间\u勾选方法中有问题:
Button CurrentButton = (Button)sender;
发送方
不是按钮
,而是计时器
那你现在要做什么
您可以在类中添加一个私有字段
private Button currentButton_;
及
及
是的,那正是崩溃发生的地方,但我如何获得当前按钮?(如果不是发件人)好的,收到了。在尝试获取名称时对其进行了轻微更改,但获得了jist。谢谢。是的,那正是崩溃发生的地方,但是我怎么才能得到当前的按钮呢?(如果不是发件人)好的,收到了。在尝试获取名称时对其进行了轻微更改,但获得了jist。谢谢