使用這個分組編輯器我們可以很容易的設計該ListVIEw控件的分組信息。
這個窗 體的加載事件處理為
/// <summary>
/// 服務已經安裝標記
/// </summary>
private bool bolServiceInstalled = false ;
private void frmVIEwLog_Load(object sender, EventArgs e)
{
try
{
System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController("MyFileSystemWatcher");
System.ServiceProcess.ServiceControllerStatus status = control.Status;
control.Dispose();
bolServiceInstalled = true;
}
catch( Exception ext )
{
lblServiceInstall.Text = "服務尚未安裝" ;
myTimer.Enabled = false ;
btnStartService.Enabled = false ;
btnStopService.Enabled = false ;
bolServiceInstalled = false ;
MessageBox.Show( this,"服務尚未安裝:" + ext.Message , "系統錯誤");
}
this.btnRefresh_Click(null, null);
}
在這裡我們首先創建一個聯系到文件監視服務的ServiceController,調用 它的Status屬性,若一切正常則表示服務已經安裝,我們設置bolServiceInstalled的標志變 量,若發生錯誤則服務尚未安裝,則顯示“服務尚未安裝”的提示信息。
對於工具條的“刷新列表”按鈕,其點擊事件處理為
private void btnRefresh_Click(object sender, EventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
lvwRecord.BeginUpdate();
try
{
lvwRecord.Items.Clear();
using (System.Data.IDbCommand cmd = Util.DBConnection.CreateCommand())
{
cmd.CommandText = "Select RecordID , ObjectName , WatchTime , EventStyle From FileSystemLog order by WatchTime";
System.Data.IDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ListViewItem NewItem = new ListVIEwItem();
NewItem.Tag = Convert.ToString(reader.GetValue (0));
NewItem.Text = Convert.ToString(reader.GetValue (1));
NewItem.SubItems.Add(Convert.ToString (reader.GetValue(2)));
string Style = Convert.ToString(reader.GetValue (3));
NewItem.SubItems.Add(Style);
Style = Style.Trim().ToLower();
if (Style == "created")
{
NewItem.Group = lvwRecord.Groups[0];
NewItem.ImageIndex = 0;
}
else if (Style == "changed")
{
NewItem.Group = lvwRecord.Groups[1];
NewItem.ImageIndex = 1;
}
else if (Style == "deleted")
{
NewItem.Group = lvwRecord.Groups[2];
NewItem.ImageIndex = 2;
}
else if (Style == "renamed")
{
NewItem.Group = lvwRecord.Groups[3];
NewItem.ImageIndex = 3;
}
NewItem.StateImageIndex = NewItem.ImageIndex;
lvwRecord.Items.Add(NewItem);
}
reader.Close();
}
myStatus.Text = "共列出 " + lvwRecord.Items.Count + " 個記錄";
}
catch (Exception ext)
{
MessageBox.Show(ext.ToString(), "系統錯誤");
}
this.Cursor = System.Windows.Forms.Cursors.Default;
lvwRecord.EndUpdate();
}