程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C#學習筆記-數據的傳遞以及ToolStripProgressBar,

C#學習筆記-數據的傳遞以及ToolStripProgressBar,

編輯:關於.NET

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     }

效果圖:(左邊為方法一的效果、右邊為方法二的效果圖)
    

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved