• C# Winform代码


    功能1:

    TextBox控件,只允许输入正负数字和小数,且小数点只能后两位

    功能2: 

    winform控件启动线程,执行任务

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Diagnostics;
    6. using System.Drawing;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. namespace WinFormsAppProject
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. ///
    20. /// winform 多个textbox只能输入小数
    21. /// 选中控件,属性,右侧有个事件按钮,在下方keypress中选择绑定的事件
    22. /// 限制输入,只能输入正负数,且小数点后两位
    23. ///
    24. ///
    25. ///
    26. private void RestrictInput(object sender, KeyPressEventArgs e)
    27. {
    28. if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
    29. {
    30. e.Handled = true;
    31. }
    32. if (e.KeyChar == (char)('-'))
    33. {
    34. if ((sender as TextBox).Text != "")
    35. {
    36. e.Handled = true;
    37. }
    38. }
    39. //第1位是负号时候、第2位小数点不可
    40. if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
    41. {
    42. e.Handled = true;
    43. }
    44. //负号只能1次
    45. if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0))
    46. e.Handled = true;
    47. //第1位小数点不可
    48. if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
    49. {
    50. e.Handled = true;
    51. }
    52. //小数点只能1次
    53. if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
    54. {
    55. e.Handled = true;
    56. }
    57. //小数点(最大到2位)
    58. if (e.KeyChar != '\b' && (((TextBox)sender).SelectionStart) > (((TextBox)sender).Text.LastIndexOf('.')) + 2 && ((TextBox)sender).Text.IndexOf(".") >= 0)
    59. e.Handled = true;
    60. //光标在小数点右侧时候判断
    61. if (e.KeyChar != '\b' && ((TextBox)sender).SelectionStart >= (((TextBox)sender).Text.LastIndexOf('.')) && ((TextBox)sender).Text.IndexOf(".") >= 0)
    62. {
    63. if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 1)
    64. {
    65. if ((((TextBox)sender).Text.Length).ToString() == (((TextBox)sender).Text.IndexOf(".") + 3).ToString())
    66. e.Handled = true;
    67. }
    68. if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 2)
    69. {
    70. if ((((TextBox)sender).Text.Length - 3).ToString() == ((TextBox)sender).Text.IndexOf(".").ToString()) e.Handled = true;
    71. }
    72. }
    73. //第1位是0,第2位必须是小数点
    74. if (e.KeyChar != (char)('.') && e.KeyChar != 8 && ((TextBox)sender).Text == "0")
    75. {
    76. e.Handled = true;
    77. }
    78. }
    79. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    80. {
    81. RestrictInput(sender, e);
    82. }
    83. private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
    84. {
    85. RestrictInput(sender, e);
    86. }
    87. private void Form1_Load(object sender, EventArgs e)
    88. {
    89. //初始化进度条
    90. progressBar1.Maximum = 100;
    91. progressBar1.Minimum = 0;
    92. progressBar1.Value = 0;
    93. }
    94. private void button1_Click(object sender, EventArgs e)
    95. {
    96. //开启任务,异步执行,线程池线程,后台线程
    97. //窗体还可以拖动,不被阻塞
    98. func3();
    99. func1();
    100. func2();
    101. }
    102. private void button2_Click(object sender, EventArgs e)
    103. {
    104. //无线程,阻塞UI线程
    105. func4();
    106. }
    107. private void button3_Click(object sender, EventArgs e)
    108. {
    109. //System.InvalidOperationException:“线程间操作无效: 从不是创建控件“progressBar1”的线程访问它。”
    110. //可以在程序入口添加 Control.CheckForIllegalCrossThreadCalls = false; //设置不捕获线程异常 解决该问题
    111. func5();
    112. }
    113. private void button4_Click(object sender, EventArgs e)
    114. {
    115. //使用this.BeginInvoke(new EventHandler(delegate{ }));执行异步访问ui线程。使用this.BeginInvoke(mi执行异步过程中不能拖动窗体
    116. func6();
    117. }
    118. private void button5_Click(object sender, EventArgs e)
    119. {
    120. //使用System.Windows.Forms.Timer();;实现异步访问ui
    121. func7();
    122. }
    123. private void button6_Click(object sender, EventArgs e)
    124. {
    125. //使用Control.CheckForIllegalCrossThreadCalls = false; 再开启任务访问ui控件实现异步,异步执行过程中拖动窗体
    126. func5();
    127. func2();
    128. }
    129. private void button7_Click(object sender, EventArgs e)
    130. {
    131. // this.BeginInvoke(mi)在创建控件的基础句柄所在线程上异步执行指定委托func6();和其他任务func2();实现异步。使用this.BeginInvoke(mi执行异步过程中不能拖动窗体
    132. func6();
    133. func2();
    134. }
    135. private void func1()
    136. {
    137. Task.Factory.StartNew(() => {
    138. for (int i = 0; i < 5; i++)
    139. {
    140. Debug.WriteLine(i);
    141. System.Threading.Thread.Sleep(1000);
    142. }
    143. });
    144. }
    145. private void func2()
    146. {
    147. //开启任务并执行
    148. Task.Factory.StartNew(() => {
    149. for (int i = 100; i < 105; i++)
    150. {
    151. Debug.WriteLine(i);
    152. System.Threading.Thread.Sleep(500);
    153. }
    154. });
    155. }
    156. private void func3()
    157. {
    158. //开启任务并执行
    159. Task.Factory.StartNew(() => {
    160. System.Threading.Thread.Sleep(5000);
    161. Debug.WriteLine(5000);
    162. });
    163. }
    164. //阻塞UI线程
    165. private void func4()
    166. {
    167. int i = 0;
    168. while (++i < 100)
    169. {
    170. System.Threading.Thread.Sleep(30);//模拟耗时操作
    171. progressBar1.Value = i;
    172. }
    173. }
    174. #region 异步访问UI控件
    175. //使用线程访问ui控件时报错:System.InvalidOperationException:“线程间操作无效: 从不是创建控件“progressBar1”的线程访问它。”
    176. //可以在程序入口添加 Control.CheckForIllegalCrossThreadCalls = false; //设置不捕获线程异常 解决该问题
    177. private void func5()
    178. {
    179. progressBar1.Value = 0;
    180. //开启任务
    181. Task task = new Task(() =>
    182. {
    183. int i = 0;
    184. while (++i < 100)
    185. {
    186. System.Threading.Thread.Sleep(30);//模拟耗时操作
    187. progressBar1.Value = i;
    188. }
    189. });
    190. task.Start();//执行任务
    191. }
    192. //使用MethodInvoker mi; this.BeginInvoke(mi)在创建控件的基础句柄所在线程上异步执行指定委托。
    193. //解决:System.InvalidOperationException:“线程间操作无效: 从不是创建控件“progressBar1”的线程访问它。”问题
    194. private void func6()
    195. {
    196. progressBar1.Value = 0;
    197. #region 使用BeginInvoke虽然解决了跨UI线程访问控件的问题,实现异步,但是涉及到控件访问的过程中,不能拖动窗体。使用 Task.Factory.StartNew()包多一层还是不能拖动窗体。。。。。
    198. //Task.Factory.StartNew(() =>
    199. //{
    200. // //MethodInvoker mi = new MethodInvoker(() =>
    201. // //{
    202. // // int i = 0;
    203. // // while (++i < 100)
    204. // // {
    205. // // System.Threading.Thread.Sleep(30);//模拟耗时操作
    206. // // progressBar1.Value = i;
    207. // // }
    208. // //});
    209. // //this.BeginInvoke(mi);
    210. // this.BeginInvoke(new EventHandler(delegate
    211. // {
    212. // int i = 0;
    213. // while (++i < 100)
    214. // {
    215. // System.Threading.Thread.Sleep(30);//模拟耗时操作
    216. // progressBar1.Value = i;
    217. // }
    218. // }));
    219. //});
    220. #endregion
    221. this.BeginInvoke(new EventHandler(delegate
    222. {
    223. int i = 0;
    224. while (++i < 100)
    225. {
    226. System.Threading.Thread.Sleep(50);//模拟耗时操作
    227. progressBar1.Value = i;
    228. }
    229. }));
    230. }
    231. #endregion
    232. #region 使用System.Windows.Forms.Timer();;实现异步访问ui
    233. System.Windows.Forms.Timer t;
    234. private void func7()
    235. {
    236. t = new System.Windows.Forms.Timer();
    237. t.Tick += new EventHandler(TimerTick);
    238. //t.Interval = 30;//默认100
    239. t.Enabled = true;
    240. }
    241. private void TimerTick(object sender, EventArgs e)
    242. {
    243. t.Stop();
    244. t.Dispose();
    245. int i = 0;
    246. while (++i < 100)
    247. {
    248. System.Threading.Thread.Sleep(50);//模拟耗时操作
    249. progressBar1.Value = i;
    250. }
    251. }
    252. #endregion
    253. }
    254. }

  • 相关阅读:
    java.io.IOException: Broken pipe
    shell脚本如何判断上一个命令执行是否成功
    【017】基于vue.js的网易云web端(实现播放、登录)(Node接口实现)
    【前端】关于Hash,MD5的学习
    循环神经网络RNN、RNNCell、GRUCell
    【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)
    Python 及 Pycharm 的安装 2023.8
    【Linux】完美解决ubuntu18.04下vi不能使用方向键和退格键
    linux系统使用达梦数据库
    小白系统初始化配置资源失败怎么办
  • 原文地址:https://blog.csdn.net/jiangyangll/article/details/138548682