时间:2021-05-20
代码:
方法一:窗体的代码-->可以直接通过预设的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; } }效果图:(左边为方法一的效果、右边为方法二的效果图)
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1.给数据库语句参数传递向数据库操作语句传递参数可以通过存储过程实现,这里给出另外两种简便易捷的方法:可以在C#中通过字符串操作将参数直接传入SQL语句变量中,
本文实例讲述了C#使用委托(delegate)实现在两个form之间传递数据的方法。分享给大家供大家参考。具体分析如下:关于Delegate【代理、委托】是C#
本文实例讲述了C#中调用MySQL存储过程的方法。分享给大家供大家参考。具体如下:这段代码演示在C#程序中调用MySQL的存储过程,没有返回值,没有参数传递。M
本文实例讲述了C#中Out与Ref的区别,可以加深C#程序设计人员对Out和Ref用法的理解,具体分析如下:一、区别分析:Out和Ref作为参数传递到方法体中,
很多时候,我们需要使用C#中的WebClient来收发数据,WebClient类提供向URI标识的任何本地、Intranet或Internet资源发送数据以及从