一、利用委托异步实现Button长按循环执行事件
来源:C# Winform 利用委托异步实现Button长按,TextBox内值累加累减,弹起停止_c# 根据按钮按下和弹起 委托-CSDN博客
bool isAddMouseDown = false; //加按钮是否按下
double thisSetValue = 0D; //textBox显示的
//+按钮鼠标按下事件 private void btnAdd_MouseDown(object sender, MouseEventArgs e) { isAddMouseDown = true; //激活按下标识 try { thisSetValue = Convert.ToDouble(txtSetValue.Text); //获取txtSetValue的初始值 } catch (Exception) { thisSetValue = 0D; } Action handler = new Action(this.Add); //定义委托 handler.BeginInvoke(null, null); //异步调用 } /// <summary> /// 累加计算 /// </summary> private void Add() { while (isAddMouseDown) { if (thisSetValue <= -1 || thisSetValue >= 50) { this.Invoke(new Action(() => MessageBox.Show("超出设定值!","警告"))); break; } else { thisSetValue += 0.1; //计算:每次累加的单位,如果要累加的精度大点,该值设定大一些 this.Invoke(new Action(() => this.txtSetValue.Text = Math.Round(thisSetValue, 2).ToString())); //界面显示 System.Threading.Thread.Sleep(100); //如果要速度块,将这个值修改小点 } } } private void btnAdd_MouseUp(object sender, MouseEventArgs e) { isAddMouseDown = false; txtHz.Text = thisValueReal.ToString(); }
标签:Button,汇总,累加,按下,Action,thisSetValue,isAddMouseDown,Winform From: https://www.cnblogs.com/Mars-0603/p/18062343