C#使用winform实现进度条效果
本文实例为大家分享了C#使用winform实现进度条效果的具体代码,供大家参考,具体内容如下
1.例子
2.点击查询按钮代码
private void button8_Click(object sender, EventArgs e) { string orgid = string.Empty; string scbz = string.Empty; if (comboBox7.SelectedIndex == -1) { orgid = ""; } else { orgid = comboBox7.SelectedItem.ToString().Substring(0, 6); } if (comboBox8.SelectedIndex == -1) { scbz = ""; } else { scbz = comboBox8.SelectedItem.ToString().Substring(0, 1); } Det_SummaryDataContext org = new Det_SummaryDataContext(); var query = from item in org.Peo_EmployeeInfo orderby item.StaID where (string.IsNullOrEmpty(orgid) || item.StaID == orgid) && (string.IsNullOrEmpty(scbz) || item.SCBZ == scbz) select item; dataGridView4.DataSource = query.ToList(); label16.Text = "共" + dataGridView4.RowCount + "条数据"; progressBar1.Value = 0;//初始化进度条的值 progressBar1.Minimum = 0; progressBar1.Maximum = dataGridView4.RowCount;//设置进度条范围 }
3.点击手动上传按钮代码
private void button7_Click(object sender, EventArgs e) { if (dataGridView4.DataSource == null) { MessageBox.Show("没有需要上传的数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { int sumcout = 0; int upcount = 0; foreach (DataGridViewRow row in dataGridView4.SelectedRows) { sumcout += 1; progressBar1.Value++;//动态显示进度条进度 Application.DoEvents();//处理当前在消息队列中的所有windows消息 string orgid = row.Cells["StaID"].Value.ToString(); string empid = row.Cells["EmpID"].Value.ToString(); Det_SummaryDataContext det = new Det_SummaryDataContext(); string cityid = (from sys in det.Base_SystemParameter select sys.CityID).ToList()[0]; root r = new root(); head h = new head(); body b = new body(); string strxml = string.Empty; string restr = string.Empty; ServiceReference1.VeptsServiceImplClient vsc = new ServiceReference1.VeptsServiceImplClient(); int evl = 0; var query1 = from ur in det.Peo_TUserRole join ri in det.Peo_RoleInfo on ur.RoleID equals ri.RoleID into A from urri in A.DefaultIfEmpty() select new { UADID = ur.UADID, RoleID = ur.RoleID, RoleName = urri.RoleName }; var query2 = from q in query1 join tu in det.Peo_TUserRightRelation on q.UADID equals tu.UADID into B from qtu in B.DefaultIfEmpty() select new { UADID = q.UADID, RoleID = q.RoleID, RoleName = q.RoleName, EmpID = qtu.EmpID }; var query = from q in query2 join ei in det.Peo_EmployeeInfo on q.EmpID equals ei.EmpID into C from qei in C.DefaultIfEmpty() where qei.EmpID == empid select new JCZ03 { idcardno = qei.IDNumber, tsno = cityid + qei.StaID.Substring(4, 2), personname = qei.EmpName, culturegrade = qei.Educational, workdate = qei.DivisionTime, //1站长 2技术负责人 3质量负责人 4外检员 5登录员 6驾控员 7尾气操作员 8其他 jobduty = q.RoleID == "130100002" ? "5" : q.RoleID == "130100003" ? "7" : q.RoleID == "130100004" ? "7" : q.RoleID == "130100005" ? "6" : q.RoleID == "130100006" ? "1" : "8", //状态(0:未审;1:在岗;2:离职,3 锁止) state = qei.EmpState == "0" ? "1" : "2" }; List<JCZ03> jcz033 = query.ToList<JCZ03>(); h.organ = Properties.Settings.Default.JKBH; h.jkxlh = Properties.Settings.Default.JKXLH; h.jkid = "JCZ03"; b.vehispara = jcz033[0]; r.head = h; r.body = b; strxml = Other.XmlSerialize<root>(r); Other.WriteLwLog(strxml);//打印发送日志 restr = System.Web.HttpUtility.UrlDecode(vsc.write(strxml)); Other.WriteLwLog(restr,false);//打印接受日志 if (!restr.Contains(@"<code>1</code>")) { evl += 1; } if (evl == 0) { var query3 = from item in det.Peo_EmployeeInfo where item.StaID == orgid && item.EmpID == empid select item; foreach (var item in query3) { item.SCBZ = "O"; } det.SubmitChanges(); upcount += 1;//上传数量 成功上传一条就自增1 } else { var query3 = from item in det.Peo_EmployeeInfo where item.StaID == orgid && item.EmpID == empid select item; foreach (var item in query3) { item.SCBZ = "E"; } det.SubmitChanges(); } } MessageBox.Show(string.Format("选中{0}条数据,成功上传{1}条!", sumcout, upcount), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
4.思路
首先设置processBar的范围,该范围就是要动态处理数据集的大小。
然后在foreach循环里设置进度条值自增,即processBar.Value++;
最后加上一句Application.DoEvents(); 用来刷新UI显示进度条进度。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)