【Winform】Button使用汇总

 


一、利用委托异步实现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(); }
复制代码

 

posted @   不溯流光  阅读(140)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-03-09 【协议】Kermit传输协议
2021-03-09 【超级终端】使用教程 WIN7
2021-03-09 【虚拟串口】VSPD虚拟串口工具
点击右上角即可分享
微信分享提示