首页 技术 正文
技术 2022年11月18日
0 收藏 607 点赞 4,645 浏览 3297 个字

代码:

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

     public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
toolStripProgressBar_save.Minimum = ;
toolStripProgressBar_save.Maximum = ;
toolStripProgressBar_save.Step = ;
} #region 不涉及数据传输
private void button_10_Click(object sender, EventArgs e)
{
//清空进度表
toolStripProgressBar_save.Value = ; if(toolStripProgressBar_save.Value<)
{
for (int i=;i<;i++)
{
toolStripProgressBar_save.PerformStep();
toolStripLabel_save.Text = toolStripProgressBar_save.Value.ToString() + "%";
}
}
} private void button_30_Click(object sender, EventArgs e)
{
if (toolStripProgressBar_save.Value < )
{
for(int i=;i<;i++)
{
toolStripProgressBar_save.PerformStep();
}
}
toolStripLabel_save.Text = "30%";
} private void button_50_Click(object sender, EventArgs e)
{
if (toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
toolStripProgressBar_save.PerformStep();
}
}
toolStripLabel_save.Text = "50%";
} private void button_60_Click(object sender, EventArgs e)
{
if (toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
toolStripProgressBar_save.PerformStep();
}
}
toolStripLabel_save.Text = "60%";
} private void button_80_Click(object sender, EventArgs e)
{
if (toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
toolStripProgressBar_save.PerformStep();
}
}
toolStripLabel_save.Text = "80%";
} private void button_100_Click(object sender, EventArgs e)
{
if (toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; 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 = ;
n.toolStripProgressBar_save.Maximum = ;
//清空进度表
n.toolStripProgressBar_save.Value = ;
n.toolStripProgressBar_save.Step = ; #region 保存过程-与单独按钮是一样的
if (n.toolStripProgressBar_save.Value < )
{ for (int i = ; i < ; i++)
{
n.toolStripProgressBar_save.PerformStep();
n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
}
} Thread.Sleep(); if (n.toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
n.toolStripProgressBar_save.PerformStep();
n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString()+"%";
}
} Thread.Sleep(); if (n.toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
n.toolStripProgressBar_save.PerformStep();
n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
}
}
Thread.Sleep(); if (n.toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
n.toolStripProgressBar_save.PerformStep();
n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
}
}
Thread.Sleep(); if (n.toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
n.toolStripProgressBar_save.PerformStep();
n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
}
}
Thread.Sleep(); if (n.toolStripProgressBar_save.Value < )
{
for (int i = ; i < ; i++)
{
n.toolStripProgressBar_save.PerformStep();
n.toolStripLabel_save.Text = n.toolStripProgressBar_save.Value.ToString() + "%";
}
}
n.toolStripLabel_save.Text = "Complete!";
Thread.Sleep();
#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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905