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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#学习笔记-数据的传递以及ToolStripProgressBar相关的知识,希望对你有一定的参考价值。

代码:

方法一:窗体的代码-->可以直接通过预设的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     }

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

 

以上是关于C#学习笔记-数据的传递以及ToolStripProgressBar的主要内容,如果未能解决你的问题,请参考以下文章

C#学习笔记_1

C# 学习笔记 6.委托

python学习笔记

WPF学习笔记之如何通过后台C#代码添加(增/删/改按钮)实现对SQLServer数据库数据的更改

最强的 C# Autofac学习笔记

C# 指针学习笔记之指针类型