工作原因,需要将进度信息显示,并可以保存所以写了个小控件,有需要的朋友可以看下
![](https://www.cnblogs.com/images/cnblogs_com/forrestsun/listInfo.jpg)
我只做了正确与错误的显示,其它的可以跟据枚举操作
环境: .NET 2.0
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Windows.Forms;
6
using System.IO;
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace ProCtrlLib
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
12
/// 描述:采用LISTVIEW形式加载消息
13
/// 作者:ForrestSun
14
/// 日期: 2006-7-17
15
/// </summary>
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
[ToolboxBitmap(typeof(ProMessageBox), "ProMessageBox")]
18
public partial class ProMessageBox : UserControl
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public ProMessageBox()
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
InitializeComponent();
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
27
private void cmnuClearInfo_Click(object sender, EventArgs e)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
ClearInfo();
30
}
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
function#region function
33
delegate void InfoWriteHandle(string text, MsgEnum MessageIcon);
34
public void MessageShow(string text, MsgEnum MessageIcon)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
37
if (this.lvwInfo.InvokeRequired)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
InfoWriteHandle iw = MessageShow;
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Invoke(iw, new object[]
{ text, MessageIcon });
41
}
42
else
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
try
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
if (text != "")
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
string strInfo = DateTime.Now.ToString() + " " + text;
49
if (MessageIcon >= 0)
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
ListViewItem objItem = new ListViewItem();
52
objItem.Text = strInfo;
53
objItem.ImageIndex = (int)MessageIcon;
54
this.lvwInfo.Items.Add(objItem);
55
this.lvwInfo.Items[this.lvwInfo.Items.Count - 1].EnsureVisible();
56
}
57
}
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
60
ItmeCountCheck();
61
}
62
catch (System.Exception ex)
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
Console.WriteLine(ex.ToString());
65
}
66
}
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
71
/// 清空显示信息
72
/// </summary>
73
private delegate void ClearInfoHandle();
74
public void ClearInfo()
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
if (this.lvwInfo.InvokeRequired)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
ClearInfoHandle clr = new ClearInfoHandle(ClearInfo);
79
this.Invoke(clr, null);
80
}
81
else
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
this.lvwInfo.Items.Clear();
84
}
85![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
ItmeCountCheck();
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
90
/// 检测LISTVIEW的ITEM个数
91
/// </summary>
92
private void ItmeCountCheck()
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
if (this.lvwInfo.Items.Count > 0)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
this.cmnuExportInfo.Enabled = true;
97
}
98
else
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
this.cmnuExportInfo.Enabled = false;
101
}
102
}
103![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
105
/// 写记录文件
106
/// </summary>
107
public void ExportToTxt()
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
if (this.SaveInfo.ShowDialog() == DialogResult.OK)
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
if (this.lvwInfo.Items.Count > 0)
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
113
try
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
string FileNmae = this.SaveInfo.FileName; //+ ".txt";
116
StreamWriter sw;
117
if (File.Exists(FileNmae))
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119
sw = File.AppendText(FileNmae);
120
}
121
else
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
sw = File.CreateText(FileNmae);
124
}
125![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
sw.WriteLine("====================记录信息====================" );
127
for (int i = 0; i < lvwInfo.Items.Count; i++)
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
129
sw.WriteLine(lvwInfo.Items[i].Text.ToString());
130
}
131
sw.Close();
132
MessageShow("信息导出成功", MsgEnum.Infomation);
133
}
134
catch (System.Exception ex)
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
MessageShow("信息导出失败,具体原因参看系统日志", MsgEnum.Error);
137
}
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
}
140
}
141
}
142
#endregion
143![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144
private void cmnuExportInfo_Click(object sender, EventArgs e)
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
this.ExportToTxt();
147
}
148
}
149![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
150
151
}
152![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
代码