C#中數(shù)據(jù)的傳遞以及ToolStripProgressBar
代碼:
方法一:窗體的代碼-->可以直接通過預(yù)設(shè)的Click事件來實(shí)現(xiàn)控制進(jìn)度條。
public partial class Form1 : Form { public Form1() { InitializeComponent(); toolStripProgressBar_save.Minimum = 0; toolStripProgressBar_save.Maximum = 100; toolStripProgressBar_save.Step = 5; } #region 不涉及數(shù)據(jù)傳輸 private void button_10_Click(object sender, EventArgs e) { //清空進(jìn)度表 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(); } }
方法二:通過調(diào)用其他類里的方法來實(shí)現(xiàn)對(duì)進(jìn)度條的控制。
注意一:需要using System.Windows.Forms;
注意二:進(jìn)度條ToolStripProgressBar的權(quán)限需要改成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; //清空進(jìn)度表 n.toolStripProgressBar_save.Value = 0; n.toolStripProgressBar_save.Step = 5; #region 保存過程-與單獨(dú)按鈕是一樣的 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 } //查找當(dāng)前打開的窗體,必須有這個(gè)才能傳遞數(shù)據(jù) 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; } }
效果圖:(左邊為方法一的效果、右邊為方法二的效果圖)
相關(guān)文章
C# winform 模擬鍵盤輸入自動(dòng)接入訪問網(wǎng)絡(luò)的實(shí)例
本篇文章主要介紹了C# winform 模擬鍵盤輸入自動(dòng)接入訪問網(wǎng)絡(luò),有興趣的可以了解一下。2016-11-11C#實(shí)現(xiàn)的三種模擬自動(dòng)登錄和提交POST信息的方法
這篇文章主要介紹了C#實(shí)現(xiàn)的三種模擬自動(dòng)登錄和提交POST信息的方法,分別列舉了WebBrowser、WebClient及HttpWebRequest實(shí)現(xiàn)自動(dòng)登錄及提交POST的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程
本文主要介紹了C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01C#調(diào)用存儲(chǔ)過程詳解(帶返回值、參數(shù)輸入輸出等)
這篇文章主要介紹了C#調(diào)用存儲(chǔ)過程的方法,結(jié)合實(shí)例形式詳細(xì)分析了各種常用的存儲(chǔ)過程調(diào)用方法,包括帶返回值、參數(shù)輸入輸出等,需要的朋友可以參考下2016-06-06C#中DataSet轉(zhuǎn)化為實(shí)體集合類的方法
這篇文章主要介紹了C#中DataSet轉(zhuǎn)化為實(shí)體集合類的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10Unity Shader實(shí)現(xiàn)3D翻頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)3D翻頁(yè)效果,Plane實(shí)現(xiàn)翻頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07