显示进度并在下面的方框中显示具体到哪一步。
界面
控件:ProgressBar、textBox、checkBox、button
名称:界面Form_progressbar、进度条progressBar、文本框textBox1、复选框checkBox1
进度条界面代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SiteWindowGIS
{
public partial class Form_progressbar : Form
{
public Form_progressbar()
{
InitializeComponent();
}
public void increaceProgressNew(int n, string textProcess)
{
progressBar.Value = Convert.ToInt32(n);//增加进度条进度
textBox1.AppendText("\r\n" + textProcess);//添加文字
textBox1.ScrollToCaret();
progressBar.CreateGraphics().DrawString(progressBar.Value.ToString() + "%", new Font("宋体", (float)12, FontStyle.Regular), Brushes.Black, new PointF(progressBar.Width / 2, progressBar.Height / 3));
if (checkBox1.Checked && progressBar.Value == 100)
{
this.Close();
}
}
private void Form_progressbar_VisibleChanged(object sender, EventArgs e)
{
}
private void Button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
}
主界面代码
楼主设计了一个界面,从主界面点击进度条,弹出进度条,测试这个是否正确。
主界面进度条部分代码
//弹出进度条窗口,初始化进度条并显示
static Form_progressbar fbnew = new Form_progressbar();
public delegate void updateProgressBarnew(int n, string text);//委托
private void foonew(updateProgressBarnew feedBack)
{
string str="";
for (int i=1;i<100;i++)
{
for (int j = 0; j < 100000000; j++) { double s = Math.Sqrt(j*1.0); }
str = "第" + i + "循环";
feedBack(i, str);
}
}
private delegate void calculateMethodnew();
private void calculatenew()
{
updateProgressBarnew ss = new updateProgressBarnew(fbnew.increaceProgressNew);
foonew(ss);
}
private void asyncnew()
{
Thread.Sleep(2000);
this.BeginInvoke(new calculateMethodnew(calculatenew));
}
private void 进度条ToolStripMenuItem_Click(object sender, EventArgs e)
{
fbnew.progressBar.Value = 0;
fbnew.progressBar.Maximum = 100;
fbnew.progressBar.Minimum = 0;
fbnew.textBox1.Text = "";
ThreadStart threadStart = new ThreadStart(asyncnew);
Thread thread = new Thread(threadStart);
thread.Start();
fbnew.ShowDialog();
}
网友评论