C#学习笔记-数据的传递以及ToolStripProgressBar

2018-06-17 22:24:44来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

代码:

方法一:窗体的代码-->可以直接通过预设的Click事件来实现控制进度条。

 1     public partial class Form1 : Form
 2     {
 3         
 4         public Form1()
 5         {
 6             InitializeComponent();
 7             toolStripProgressBar_save.Minimum = 0;
 8             toolStripProgressBar_save.Maximum = 100;
 9             toolStripProgressBar_save.Step = 5;
10         }
11 
12         #region 不涉及数据传输
13         private void button_10_Click(object sender, EventArgs e)
14         {
15             //清空进度表
16             toolStripProgressBar_save.Value = 0;
17 
18             if(toolStripProgressBar_save.Value<10)
19             {
20                 for (int i=0;i<2;i++)
21                 {
22                     toolStripProgressBar_save.PerformStep();
23                     toolStripLabel_save.Text = toolStripProgressBar_save.Value.ToString() + "%";
24                 }
25             }
26         }
27 
28         private void button_30_Click(object sender, EventArgs e)
29         {
30             if (toolStripProgressBar_save.Value < 30)
31             {
32                 for(int i=0;i<4;i++)
33                 {
34                     toolStripProgressBar_save.PerformStep();
35                 }
36             }
37             toolStripLabel_save.Text = "30%";
38         }
39 
40         private void button_50_Click(object sender, EventArgs e)
41         {
42             if (toolStripProgressBar_save.Value < 50)
43             {
44                 for (int i = 0; i < 4; i++)
45                 {
46                     toolStripProgressBar_save.PerformStep();
47                 }
48             }
49             toolStripLabel_save.Text = "50%";
50         }
51 
52         private void button_60_Click(object sender, EventArgs e)
53         {
54             if (toolStripProgressBar_save.Value < 60)
55             {
56                 for (int i = 0; i < 2; i++)
57                 {
58                     toolStripProgressBar_save.PerformStep();
59                 }
60             }
61             toolStripLabel_save.Text = "60%";
62         }
63 
64         private void button_80_Click(object sender, EventArgs e)
65         {
66             if (toolStripProgressBar_save.Value < 80)
67             {
68                 for (int i = 0; i < 4; i++)
69                 {
70                     toolStripProgressBar_save.PerformStep();
71                 }
72             }
73             toolStripLabel_save.Text = "80%";
74         }
75 
76         private void button_100_Click(object sender, EventArgs e)
77         {
78             if (toolStripProgressBar_save.Value < 100)
79             {
80                 for (int i = 0; i < 4; i++)
81                 {
82                     toolStripProgressBar_save.PerformStep();
83                 }               
84             }
85             toolStripLabel_save.Text = "Complete!";
86         }
87         #endregion
88 
89 
90         private void button_save_Click(object sender, EventArgs e)
91         {
92             Save.Singleton().SaveAll();
93         }
94     }

方法二:通过调用其他类里的方法来实现对进度条的控制。

注意一:需要using System.Windows.Forms;

注意二:进度条ToolStripProgressBar的权限需要改成Public

  1    public class Save
  2     {
  3         private static Save _instance = null;
  4 
  5         private Form1 n = null;
  6 
  7         public void SaveAll()
  8         {
  9             getWnd();
 10 
 11             n.toolStripProgressBar_save.Minimum = 0;
 12             n.toolStripProgressBar_save.Maximum = 100;
 13             //清空进度表
 14             n.toolStripProgressBar_save.Value = 0;
 15             n.toolStripProgressBar_save.Step = 5;
 16 
 17             #region 保存过程-与单独按钮是一样的
 18             if (n.toolStripProgressBar_save.Value < 10)
 19             {
 20                 
 21                 for (int i = 0; i < 2; i++)
 22                 {
 23                     n.toolStripProgressBar_save.PerformStep();
 24                     n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
 25                 }
 26             }
 27             
 28             Thread.Sleep(1000);
 29 
 30             if (n.toolStripProgressBar_save.Value < 30)
 31             {
 32                 for (int i = 0; i < 4; i++)
 33                 {
 34                     n.toolStripProgressBar_save.PerformStep();
 35                     n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString()+"%";
 36                 }
 37             }
 38             
 39 
 40             Thread.Sleep(100);
 41 
 42             if (n.toolStripProgressBar_save.Value < 50)
 43             {
 44                 for (int i = 0; i < 4; i++)
 45                 {
 46                     n.toolStripProgressBar_save.PerformStep();
 47                     n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
 48                 }
 49             }
 50             Thread.Sleep(100);
 51 
 52             if (n.toolStripProgressBar_save.Value < 60)
 53             {
 54                 for (int i = 0; i < 2; i++)
 55                 {
 56                     n.toolStripProgressBar_save.PerformStep();
 57                     n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
 58                 }
 59             }
 60             Thread.Sleep(100);
 61 
 62             if (n.toolStripProgressBar_save.Value < 80)
 63             {
 64                 for (int i = 0; i < 4; i++)
 65                 {
 66                     n.toolStripProgressBar_save.PerformStep();
 67                     n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
 68                 }
 69             }
 70             Thread.Sleep(100);
 71 
 72             if (n.toolStripProgressBar_save.Value < 100)
 73             {
 74                 for (int i = 0; i < 4; i++)
 75                 {
 76                     n.toolStripProgressBar_save.PerformStep();
 77                     n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
 78                 }
 79             }
 80             n.toolStripLabel_save.Text = "Complete!";
 81             Thread.Sleep(100);
 82             #endregion
 83 
 84         }
 85 
 86         //查找当前打开的窗体,必须有这个才能传递数据
 87         private void getWnd()
 88         {
 89             foreach(Form fm in Application.OpenForms)
 90             {
 91                 if (fm.Name == "Form1")
 92                 {
 93                     n = (Form1)fm;
 94                     break;
 95                 }
 96             }
 97         }
 98 
 99         public static Save Singleton()
100         {
101             if (_instance == null)
102             {
103                 _instance = new Save();
104             }
105             return _instance;
106         }
107     }

效果图:(左边为方法一的效果、右边为方法二的效果图)
    

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:WebForm(一)——IIS服务器、开发方式和简单基础

下一篇:复合控件