[英]How can I make a winform button “do something” as long as I am pressing the button?
I have tried all day, and looked up all kinds of ideas... with no real help.我已经尝试了一整天,并查找了各种想法......没有真正的帮助。 When I press a button, like "JOG", which would move a CNC Machine axis continuously, as long a the button is pressed, then when released, it would stop.
当我按下一个按钮时,比如“JOG”,它会连续移动数控机床轴,只要按下按钮,然后松开,它就会停止。
To test this I am using a "picuture / LED" which when I press and hold, should be on... and when I release, it should turn off.为了测试这一点,我使用了一个“图片/LED”,当我按住它时,它应该打开……当我松开时,它应该关闭。
Pressed Button should = only while pressed, do something.按下按钮应该 = 仅在按下时执行某些操作。 Release of same button = stop doing whatever you were doing now.
释放相同的按钮 = 停止做你现在正在做的任何事情。
I am sure for you advanced folks, this is maybe 101... but for me... it is eating my lunch... help?我敢肯定,对于你们这些先进的人来说,这可能是 101...但对我来说...它正在吃我的午餐...帮助?
You can use the MouseDown
and MouseUp
events.您可以使用
MouseDown
和MouseUp
事件。 When the MouseDown
event is hit, call a method that loops and performs your action.当
MouseDown
事件被击中时,调用一个循环并执行您的操作的方法。 Once MouseUp
is hit, stop the loop.一旦
MouseUp
被击中,停止循环。
private bool _run = false;
public void button_MouseDown(object sender, EventArgs e)
{
_run = true;
MyAction();
}
public void button_MouseUp(object sender, EventArgs e)
{
_run = false;
}
public void MyAction()
{
while(_run)
{
//You actions
}
}
Note that the above example will hog up the UI thread.请注意,上面的示例将占用 UI 线程。 You should run it on another thread using a
BackgroundWorker
or something similar.您应该使用
BackgroundWorker
或类似的东西在另一个线程上运行它。
Generically, have a look at the mouse up and down events.一般来说,看看鼠标向上和向下事件。 I would have it call some function asynchronously (not on the UI thread) when the mouse is down.
当鼠标按下时,我会让它异步调用一些函数(不在 UI 线程上)。 And stop it when the mouse up event fires.
并在鼠标向上事件触发时停止它。 System.Threading has some nice models for this.
System.Threading 为此提供了一些不错的模型。 Try googling around there.
尝试在那里谷歌搜索。
You are wanting to start and stop a thread where the procedure is looping performing your action.您想要启动和停止一个线程,其中程序正在循环执行您的操作。
I'd make my own subclass, something like this:我会创建自己的子类,如下所示:
public class RepeatButton : Button
{
readonly Timer timer = new Timer();
public event EventHandler Depressed;
public virtual TimeSpan Interval
{
get { return TimeSpan.FromMilliseconds(timer.Interval); }
set { timer.Interval = (int)value.TotalMilliseconds; }
}
public RepeatButton()
{
timer.Interval = 100;
timer.Tick += delegate { OnDepressed(); };
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
timer.Stop();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
timer.Start();
}
protected virtual void OnDepressed()
{
var handler = this.Depressed;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
This allows your code to be asynchronous but also the Depressed
event would be invoked on the UI thread still.这允许您的代码是异步的,但仍然会在 UI 线程上调用
Depressed
事件。
Thanks all, this is about as simple as I could get it.谢谢大家,这和我能得到的一样简单。 Button and Mouse control mixed togather, needs mouse handling... which is added in the button properties, which will add the code to the designer.
Button 和 Mouse 控件混在一起,需要鼠标处理...这是在按钮属性中添加的,它将代码添加到设计器中。
private void button2_MouseDown(object sender, MouseEventArgs e)
{
led18.Show();
}
private void button2_MouseUp(object sender, MouseEventArgs e)
{
led18.Hide();
}
//below get automatically put into the design file...
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
Consider Space bar to trigger button down and up as well.考虑空格键也可以向下和向上触发按钮。
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button1_KeyDown);
this.button1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.button1_KeyUp);
private void button1_MouseDown(object sender, MouseEventArgs e)
{
led18.Show();
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
led18.Hide();
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Space && e.Alt==false && e.Control==false && e.Shift==false)
{
led18.Show();
}
}
private void button1_KeyUp(object sender, KeyEventArgs e)
{
led18.Hide();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.