【文件属性】:
文件名称:自定义的类实现状态栏时间和网络状态的变化
文件大小:2KB
文件格式:TXT
更新时间:2014-09-27 11:14:44
委托,事件,类,状态栏 自动刷新
自定义的类实现状态栏时间和网络状态的变化
//qjh2008 by 2011-08
//事件,委托,自动更新,类,oo
//更新状态栏
public delegate void UpdateStatusStrip(int index,string str);
//主窗体中
private void UpdateStatus(int Index, string str)
{
if (InvokeRequired)
Invoke(new UpdateStatusStrip(UpdateStatus), Index, str);
else
{
statusStrip1.Items[Index].Text = str;
}
}
//自定义的状态栏
class MyStatusStrip
{
//
System.Timers.Timer myTimer;
public event UpdateStatusStrip UpdateStatus;
MyClient MastClient;//网络连接客户端
///
/// 构造函数
///
///
public MyStatusStrip(StatusStrip strip,MyClient client)
{
MastClient = client;
InitCtrl();
}
~MyStatusStrip()
{
myTimer.Enabled = false;
}
///网络状态
public string netStatus
{
get
{
string re = "";
if (MastClient == null)
{
return "未连接";
}
switch (MastClient.myState)
{
case 0:
re = "无连接";
break;
case 1:
re = "已连接";
break;
case 2:
re = "忙碌";
break;
}
return re;
}
}
void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//间隔事件
// ToolStripItem myitem6 = new ToolStripStatusLabel(System.DateTime.Now.ToShortTimeString());
//myStatus.Items[5].Text = System.DateTime.Now.ToShortTimeString();
//myStatus.Items[7].Text = netStatus;
UpdateStatus(5, System.DateTime.Now.ToShortTimeString());
UpdateStatus(7, netStatus);
}
///
/// 初始化控件
///
private void InitCtrl()
{
try
{
myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
}
catch (Exception er)
{
}
}
}