August 15, 2008

ActiveX Stopwatch Timer Control

Here's an example of a StopWatch control implemented in Csharp and used as a control on a web client. The control needs to be register on the clients machine then added to the clients web application (in html) and then called from Javascript.

<OBJECT id="activeXTimerControl" name="activeXTimerControl" 
        classid="clsid:4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7" 
        style="width: 504px; height: 182px" 
       codebase="Bin/ActiveXTimerControl.dll" class="ActiveXTimerControl.TimerControl">
       </OBJECT>

To start the Stopwatch call Start() and to end call End() then call Time() to return the result as a string or use the AddTimeToGrid to add the time to a DataGrid Control (supplied by the StopWatch control) or call AddTimeToLabel which adds the time to a textbox.
activeXTimerControl.Start();
///do someting
activeXTimerControl.Stop();
var time = activeXTimerControl.Time;
activeXTimerControl.AddTimeToGrid("my timer", time);
activeXTimerControl.AddTimeToLabel("my timer", time);

Here's the full csharp code and at the end the full html


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace ActiveXTimerControl
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7")]
public partial class TimerControl : UserControl, IActiveXTimerControl
{
public TimerControl()
{
InitializeComponent();
}
}
}


namespace ActiveXTimerControl
{
public interface IActiveXTimerControl
{
///
/// Add resulting timer name and time (as a string) to the GridView.
///
///
///
void AddTimeToGrid(string timerName, string time);
///
/// Add the resulting timer name and the time (as a string) to the Label.
///
///
///
void AddTimeToLabel(string timerName, string time);
///
/// Start the timer.
///
void Start();
///
/// Stop the timer, call Start() before this.
///
void Stop();
///
/// Get the Time between the Start() and Stop() in milliseconds.
///
string Time { get; }
}

partial class TimerControl : IActiveXTimerControl
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this._timerDataGridView = new System.Windows.Forms.DataGridView();
this._timerNameColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this._timerTimeColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this._timerLabel = new System.Windows.Forms.Label();
this._timerTextBox = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this._timerDataGridView)).BeginInit();
this.SuspendLayout();
//
// _timerDataGridView
//
this._timerDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this._timerDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this._timerNameColumn,
this._timerTimeColumn});
this._timerDataGridView.Location = new System.Drawing.Point(6, 108);
this._timerDataGridView.Name = "_timerDataGridView";
this._timerDataGridView.Size = new System.Drawing.Size(261, 106);
this._timerDataGridView.TabIndex = 0;
//
// _timerNameColumn
//
this._timerNameColumn.HeaderText = "Timer Name";
this._timerNameColumn.Name = "_timerNameColumn";
//
// _timerTimeColumn
//
this._timerTimeColumn.HeaderText = "Time (msec)";
this._timerTimeColumn.Name = "_timerTimeColumn";
//
// _timerLabel
//
this._timerLabel.AutoSize = true;
this._timerLabel.Location = new System.Drawing.Point(223, 78);
this._timerLabel.Name = "_timerLabel";
this._timerLabel.Size = new System.Drawing.Size(129, 13);
this._timerLabel.TabIndex = 1;
this._timerLabel.Text = "Timer Name | Time (msec)";
//
// _timerTextBox
//
this._timerTextBox.Location = new System.Drawing.Point(6, 20);
this._timerTextBox.Multiline = true;
this._timerTextBox.Name = "_timerTextBox";
this._timerTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this._timerTextBox.Size = new System.Drawing.Size(261, 55);
this._timerTextBox.TabIndex = 2;
this._timerTextBox.Text = "Timer Name | Time (msec)";
//
// TimerControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this._timerTextBox);
this.Controls.Add(this._timerLabel);
this.Controls.Add(this._timerDataGridView);
this.Name = "TimerControl";
this.Size = new System.Drawing.Size(383, 313);
((System.ComponentModel.ISupportInitialize)(this._timerDataGridView)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}



#endregion



public void Start()
{
if (_stopWatch == null)
_stopWatch = new System.Diagnostics.Stopwatch();
_stopWatch.Start();
}

public void Stop()
{
_stopWatch.Stop();
}
public string Time
{
get
{
return string.Format("{0}", _stopWatch.ElapsedMilliseconds);
}
}

private System.Diagnostics.Stopwatch _stopWatch;

private System.Windows.Forms.DataGridView _timerDataGridView;
public void AddTimeToGrid(string timerName, string time)
{
if (_timerDataGridView == null)
_timerDataGridView = new System.Windows.Forms.DataGridView();
_timerDataGridView.Rows.Add(timerName, time);
this.Refresh();

}

private System.Windows.Forms.DataGridViewTextBoxColumn _timerNameColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn _timerTimeColumn;

private System.Windows.Forms.Label _timerLabel;
public void AddTimeToLabel(string timerName, string time)
{
//if (_timerLabel == null)
// _timerLabel = new System.Windows.Forms.Label();
//_timerLabel.Text += ( "\n" + timerName + " " + time);
//this.Refresh();
if (_timerTextBox == null)
{
_timerTextBox = new System.Windows.Forms.TextBox();
_timerTextBox.Text = "Timer Name | Time (msec)";
}
_timerTextBox.Text += ("\r\n" + timerName + " " + time);
this.Refresh();
}

private System.Windows.Forms.TextBox _timerTextBox;

}

}




The aspx:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>

<OBJECT id="activeXTimerControl" name="activeXTimerControl" classid="clsid:4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7" style="width: 504px; height: 182px" codebase="Bin/ActiveXTimerControl.dll" class="ActiveXTimerControl.TimerControl">
</OBJECT>


<form name="frm" id="frm">
<input type=button value="Click me" onClick="doScript();">
</form>


</body>

<script language="javascript">
function doScript()
{
activeXTimerControl.Start();

activeXTimerControl.Stop();
var time = activeXTimerControl.Time;
activeXTimerControl.AddTimeToGrid("my timer", time);
activeXTimerControl.AddTimeToLabel("my timer", time);




}
</script>

</html>



No comments: