简体   繁体   English

只要我按下按钮,如何让 winform 按钮“做某事”?

[英]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.您可以使用MouseDownMouseUp事件。 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.

相关问题 当我按下按钮时,CheckBox不起作用 - CheckBox is not working when I am pressing the button 按下提交按钮后,如何重定向到下一页? - How do I redirect to the next page after pressing the submit button? 如何从Winform的Button调用WPF命令? - How can I call a WPF command from a Winform's Button? (C#)如何在按下按钮时显示新表格窗口? 我可以将其他按钮和文本框拖到其中的一个 - (C#)How do I, upon pressing a button, Make a new form window appear? One that i can drag other buttons and text boxes onto 仅在按下按钮后才能显示TextBlock? - How can I display a TextBlock only after pressing a button? 如何设置我的winform按钮,以便我可以单击按钮或按Enter键 - How can I set up my winform button so that I can either click the button or press enter 当按下按钮而不是按住按钮时,如何让事情发生一次? - How do I make something happen ONCE when a button pressed but not WHILE it is held? 我希望在用户在文本框中键入某些内容之前,发送按钮不起作用。 我怎样才能做到这一点? - I would like the send button to be inactive until the user key in something in the textbox. How can I do this? 如何在游戏视图中隐藏ui按钮并在按退出键时显示该按钮? - How can i hide in game view a ui button and show the button when pressing the escape key? 致电WebBrowser.Refresh之后,我应该等待多长时间才能执行某项操作? - How long am I supposed to wait to do something after I call WebBrowser.Refresh?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM