C#中数据的传递以及ToolStripProgressBar

时间:2022-04-30 07:43:56

代码:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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#中数据的传递以及ToolStripProgressBar    C#中数据的传递以及ToolStripProgressBar