栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#中数据的传递以及ToolStripProgressBar

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#中数据的传递以及ToolStripProgressBar

代码:

方法一:窗体的代码-->可以直接通过预设的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;
  }
 }

 

效果图:(左边为方法一的效果、右边为方法二的效果图)

   

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号