johngeng 2007年12月24日 1:34:27
.Net 下ProgressBar控件可以让程序在加载数据时让用户知道目前的进度.诸如安装程序,加载数据到Listview中等.下面就以ProgressBar控件在Listview下的应用为例.
2.界面部分(红色框部分为此例中用到的控件)
3.编辑代码部分
初始化ListView如:
private void initialListView()
{
lvRentList.Columns.Add("租赁编号", 60);
lvRentList.Columns.Add("会员编号", 60);
lvRentList.Columns.Add("租赁金额", 70);
lvRentList.Columns.Add("折扣率", 70);
lvRentList.Columns.Add("总金额", 70);
lvRentList.Columns.Add("租赁日期", 120);
lvRentList.GridLines = true;
lvRentList.View = View.Details;
lvRentList.FullRowSelect = true;
}
在申明部分创建个委托(delegate){
lvRentList.Columns.Add("租赁编号", 60);
lvRentList.Columns.Add("会员编号", 60);
lvRentList.Columns.Add("租赁金额", 70);
lvRentList.Columns.Add("折扣率", 70);
lvRentList.Columns.Add("总金额", 70);
lvRentList.Columns.Add("租赁日期", 120);
lvRentList.GridLines = true;
lvRentList.View = View.Details;
lvRentList.FullRowSelect = true;
}
private delegate void dispProgStatus(string state);
用于更新ProgressBar的method.其中参数 state用于显示进度(当前及总共) private void showProgStatus(string state)
{
string type = "";
if (progBarReport.InvokeRequired)
progBarReport.Invoke(new dispProgStatus(showProgStatus), state);
else
{
type = state.Substring(0, state.IndexOf("") + 2);
string[] data = state.Substring(state.IndexOf("") + 3).Split('/');
int current = 0;
Int32.TryParse(data[0], out current);
int total = 0;
Int32.TryParse(data[1], out total);
progBarReport.Value = current;
progBarReport.Maximum = total;
}
if (lblProgStatus.InvokeRequired)
lblProgStatus.Invoke(new dispProgStatus(showProgStatus), state);
else
lblProgStatus.Text = state;
}
载入数据到ListView中的method 其中参数DataTable dtRent是用于传递要载入到ListView中的数据 {
string type = "";
if (progBarReport.InvokeRequired)
progBarReport.Invoke(new dispProgStatus(showProgStatus), state);
else
{
type = state.Substring(0, state.IndexOf("") + 2);
string[] data = state.Substring(state.IndexOf("") + 3).Split('/');
int current = 0;
Int32.TryParse(data[0], out current);
int total = 0;
Int32.TryParse(data[1], out total);
progBarReport.Value = current;
progBarReport.Maximum = total;
}
if (lblProgStatus.InvokeRequired)
lblProgStatus.Invoke(new dispProgStatus(showProgStatus), state);
else
lblProgStatus.Text = state;
}
private void loadRentListView(DataTable dtRent,string state)
{
lvRentList.Items.Clear();
ArrayList memberList = new ArrayList();
decimal rentTotal = 0M;
ArrayList itemList = new ArrayList();
string type = state.ToString();
ListViewItem[] rentItems = new ListViewItem[dtRent.Rows.Count]; //将用于整体插入ListView中可以大幅提高效率
for (int i = 0; i < dtRent.Rows.Count; i++)
{
if (i == dtRent.Rows.Count - 1)
type = "Loading Data Completed";
state = type + (i + 1).ToString() + "/" + dtRent.Rows.Count.ToString();
int sessionID = 0;
Int32.TryParse(dtRent.Rows[i].ItemArray.GetValue(0).ToString(), out sessionID);
int mid = 0;
Int32.TryParse(dtRent.Rows[i].ItemArray.GetValue(1).ToString(), out mid);
if (!memberList.Contains(mid))
memberList.Add(mid);
Decimal discount = 0M;
Decimal.TryParse(dtRent.Rows[i].ItemArray.GetValue(3).ToString(), out discount);
discount = Decimal.Round(discount * 100M, 2);
Decimal total = 0M;
Decimal.TryParse(dtRent.Rows[i].ItemArray.GetValue(4).ToString(), out total);
rentTotal += total;
DateTime rentDate = DateTime.Now;
DateTime.TryParse(dtRent.Rows[i].ItemArray.GetValue(6).ToString(), out rentDate);
string[] items = { sessionID.ToString(), mid.ToString(), "$" + dtRent.Rows[i].ItemArray.GetValue(2).ToString(), "$" +discount.ToString() + "%"
, "$" + total.ToString(), rentDate.ToShortDateString() };
rentItems[i] = new ListViewItem(items) //将每条数据载入到ListViewItem中
showProgStatus(state.ToString()); //用于显示ProgressBar的进度
}
lvRentList.Items.AddRange(rentItems); //批量插入数据到List View中,曾经试验过Add 比 AddRange的效率低百倍
txtRentItemCount.Text = itemList.Count.ToString();
txtRentMemberCount.Text = memberList.Count.ToString();
txtRentFeeCount.Text = "$" + rentTotal.ToString();
}
取出数据并调用载入数据到ListView的method其中参数object ThreadPool的静态method所要传递的参数,可以用来封装数据,未方便我基本用来传递进度{
lvRentList.Items.Clear();
ArrayList memberList = new ArrayList();
decimal rentTotal = 0M;
ArrayList itemList = new ArrayList();
string type = state.ToString();
ListViewItem[] rentItems = new ListViewItem[dtRent.Rows.Count]; //将用于整体插入ListView中可以大幅提高效率
for (int i = 0; i < dtRent.Rows.Count; i++)
{
if (i == dtRent.Rows.Count - 1)
type = "Loading Data Completed";
state = type + (i + 1).ToString() + "/" + dtRent.Rows.Count.ToString();
int sessionID = 0;
Int32.TryParse(dtRent.Rows[i].ItemArray.GetValue(0).ToString(), out sessionID);
int mid = 0;
Int32.TryParse(dtRent.Rows[i].ItemArray.GetValue(1).ToString(), out mid);
if (!memberList.Contains(mid))
memberList.Add(mid);
Decimal discount = 0M;
Decimal.TryParse(dtRent.Rows[i].ItemArray.GetValue(3).ToString(), out discount);
discount = Decimal.Round(discount * 100M, 2);
Decimal total = 0M;
Decimal.TryParse(dtRent.Rows[i].ItemArray.GetValue(4).ToString(), out total);
rentTotal += total;
DateTime rentDate = DateTime.Now;
DateTime.TryParse(dtRent.Rows[i].ItemArray.GetValue(6).ToString(), out rentDate);
string[] items = { sessionID.ToString(), mid.ToString(), "$" + dtRent.Rows[i].ItemArray.GetValue(2).ToString(), "$" +discount.ToString() + "%"
, "$" + total.ToString(), rentDate.ToShortDateString() };
rentItems[i] = new ListViewItem(items) //将每条数据载入到ListViewItem中
showProgStatus(state.ToString()); //用于显示ProgressBar的进度
}
lvRentList.Items.AddRange(rentItems); //批量插入数据到List View中,曾经试验过Add 比 AddRange的效率低百倍
txtRentItemCount.Text = itemList.Count.ToString();
txtRentMemberCount.Text = memberList.Count.ToString();
txtRentFeeCount.Text = "$" + rentTotal.ToString();
}
private void dispRentReport(object state)
{
DateTime startDate = dtpStartDate.Value.Date;
DateTime endDate = dtpEndDate.Value.Date.AddDays(1);
Session mySession = new Session();
dtRent = mySession.getSessionsByDate(startDate, endDate, 0);
loadRentListView(dtRent,state.ToString());
}
按钮btnSearch的click事件 点击按钮后即查找并将数据载入到ListView中{
DateTime startDate = dtpStartDate.Value.Date;
DateTime endDate = dtpEndDate.Value.Date.AddDays(1);
Session mySession = new Session();
dtRent = mySession.getSessionsByDate(startDate, endDate, 0);
loadRentListView(dtRent,state.ToString());
}
private void btnSearch_Click(object sender, EventArgs e)
{
Console.WriteLine("Cat Index:" + comboSearchCat.SelectedIndex);
//Date Range
if (comboSearchCat.SelectedIndex == -1)
{
object state = "Loading Rent Data";
ThreadPool.QueueUserWorkItem(dispRentReport, state);
}
else
{
object state = "Loading Items under Category ";
ThreadPool.QueueUserWorkItem(dispRentReportByCategory, state);
}
btnSearch.Enabled = false;
}
运行结果如图: (图1 正在加载数据,并可以看到ListView中为空白.图2 显示数据加载完毕后才加载到ListView中){
Console.WriteLine("Cat Index:" + comboSearchCat.SelectedIndex);
//Date Range
if (comboSearchCat.SelectedIndex == -1)
{
object state = "Loading Rent Data";
ThreadPool.QueueUserWorkItem(dispRentReport, state);
}
else
{
object state = "Loading Items under Category ";
ThreadPool.QueueUserWorkItem(dispRentReportByCategory, state);
}
btnSearch.Enabled = false;
}
(图1)
(图2)