C#中数据的传递以及ToolStripProgressBar
程序员文章站
2022-03-26 08:36:43
代码:
方法一:窗体的代码-->可以直接通过预设的click事件来实现控制进度条。
public partial class form1 : form...
代码:
方法一:窗体的代码-->可以直接通过预设的click事件来实现控制进度条。
public partial class form1 : form { public form1() { initializecomponent(); toolstripprogressbar_save.minimum = 0; toolstripprogressbar_save.maximum = 100; toolstripprogressbar_save.step = 5; } #region 不涉及数据传输 private void button_10_click(object sender, eventargs e) { //清空进度表 toolstripprogressbar_save.value = 0; if(toolstripprogressbar_save.value<10) { for (int i=0;i<2;i++) { toolstripprogressbar_save.performstep(); toolstriplabel_save.text = toolstripprogressbar_save.value.tostring() + "%"; } } } private void button_30_click(object sender, eventargs e) { if (toolstripprogressbar_save.value < 30) { for(int i=0;i<4;i++) { toolstripprogressbar_save.performstep(); } } toolstriplabel_save.text = "30%"; } private void button_50_click(object sender, eventargs e) { if (toolstripprogressbar_save.value < 50) { for (int i = 0; i < 4; i++) { toolstripprogressbar_save.performstep(); } } toolstriplabel_save.text = "50%"; } private void button_60_click(object sender, eventargs e) { if (toolstripprogressbar_save.value < 60) { for (int i = 0; i < 2; i++) { toolstripprogressbar_save.performstep(); } } toolstriplabel_save.text = "60%"; } private void button_80_click(object sender, eventargs e) { if (toolstripprogressbar_save.value < 80) { for (int i = 0; i < 4; i++) { toolstripprogressbar_save.performstep(); } } toolstriplabel_save.text = "80%"; } private void button_100_click(object sender, eventargs e) { if (toolstripprogressbar_save.value < 100) { for (int i = 0; i < 4; i++) { toolstripprogressbar_save.performstep(); } } toolstriplabel_save.text = "complete!"; } #endregion private void button_save_click(object sender, eventargs e) { save.singleton().saveall(); } }
方法二:通过调用其他类里的方法来实现对进度条的控制。
注意一:需要using system.windows.forms;
注意二:进度条toolstripprogressbar的权限需要改成public
public class save { private static save _instance = null; private form1 n = null; public void saveall() { getwnd(); n.toolstripprogressbar_save.minimum = 0; n.toolstripprogressbar_save.maximum = 100; //清空进度表 n.toolstripprogressbar_save.value = 0; n.toolstripprogressbar_save.step = 5; #region 保存过程-与单独按钮是一样的 if (n.toolstripprogressbar_save.value < 10) { for (int i = 0; i < 2; i++) { n.toolstripprogressbar_save.performstep(); n.toolstriplabel_save.text = n.toolstripprogressbar_save.value.tostring() + "%"; } } thread.sleep(1000); if (n.toolstripprogressbar_save.value < 30) { for (int i = 0; i < 4; i++) { n.toolstripprogressbar_save.performstep(); n.toolstriplabel_save.text = n.toolstripprogressbar_save.value.tostring()+"%"; } } thread.sleep(100); if (n.toolstripprogressbar_save.value < 50) { for (int i = 0; i < 4; i++) { n.toolstripprogressbar_save.performstep(); n.toolstriplabel_save.text = n.toolstripprogressbar_save.value.tostring() + "%"; } } thread.sleep(100); if (n.toolstripprogressbar_save.value < 60) { for (int i = 0; i < 2; i++) { n.toolstripprogressbar_save.performstep(); n.toolstriplabel_save.text = n.toolstripprogressbar_save.value.tostring() + "%"; } } thread.sleep(100); if (n.toolstripprogressbar_save.value < 80) { for (int i = 0; i < 4; i++) { n.toolstripprogressbar_save.performstep(); n.toolstriplabel_save.text = n.toolstripprogressbar_save.value.tostring() + "%"; } } thread.sleep(100); if (n.toolstripprogressbar_save.value < 100) { for (int i = 0; i < 4; i++) { n.toolstripprogressbar_save.performstep(); n.toolstriplabel_save.text = n.toolstripprogressbar_save.value.tostring() + "%"; } } n.toolstriplabel_save.text = "complete!"; thread.sleep(100); #endregion } //查找当前打开的窗体,必须有这个才能传递数据 private void getwnd() { foreach(form fm in application.openforms) { if (fm.name == "form1") { n = (form1)fm; break; } } } public static save singleton() { if (_instance == null) { _instance = new save(); } return _instance; } }
效果图:(左边为方法一的效果、右边为方法二的效果图)
上一篇: C#获取文件MD5值的实现示例