Pocket PC TimeBox Control
By Fons Sonnemans (27 May 2003)
Download
TimeBox.zip
Introduction
I own a Pocket PC since a few months. It's a nice peace of hardware although I didn't
use it much. There wasn't much practical use for it. Now I have decided to write
my own Time Registration and maybe my own Car Mileage Administration application.
In both Smart Device Applications I have to enter a Time regullary. Microsoft didn't
supply me with a good control for this so I decided to write one.
Writing a Smart Device Applications is made easy with the introduction of Visual
Studio.NET 2003 and the .NET Compact Framework. In this article I describe how I
have written a TimeBox control for the Pocket PC. It's not much different from a
normal Windows Forms control.

TimeBox Sample running in the Pocket PC Emulator
I plan to use this TimeBox in my application so I have placed it in an separate
project. Due to the low memory footprint you also have to create a designer able
version of it. This version will have extra design-time only code (methods and attributes).
Controls Project
 |
The ReflectionIT.Windows.Forms project consists of 3 controls: UpDownButton, SpinButton
and TimeBox. The UpDownButton is used by the SpinButton control to draw a Up and
a Down button. The SpinButton is used by the TimeBox to spin the hours and minutes.
Once you have compiled your control you need to copy it to specific locations so
that Microsoft Visual Studio.NET and the .NET Compact Framework can find it. The
runtime version of your control should be copied to the C:\Program Files\Microsoft
Visual Studio .NET\CompactFrameworkSDK\v1.0.5000\Windows CE\ directory if you have
done the default install.
I have created a special batch file (copytowince 2003.cmd) which compiles the controls
project and the designer project and copies the DLL's to the correct folders.
|
Time class
The Time class is used to store the Hours and Minutes in. It supports arithmetic,
comparison and conversion operator overloading.
Time t1 = new Time(10, 45);
Time t2 = new Time(0, 15);
Time t3 = t1 +
t2;
if (t3
< (12
* 60))
{
Console.WriteLine(String.Format("Good
morning, it is {0}",
t3));
}
The Time class has a TypeConverter which is used to support designer serialization.
public override
object ConvertTo(ITypeDescriptorContext
context, System.Globalization.CultureInfo
culture,
object value,
Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo
ci = typeof(Time).GetConstructor(new Type[]{typeof(int), typeof(int)});
Time t =
(Time)value;
return
new InstanceDescriptor(ci,new
object[]{t.Hours,
t.Minutes});
}
if (destinationType.Equals(typeof(String)))
{
return
value.ToString();
} else
{
return
base.ConvertTo (context,
culture,
value, destinationType);
}
}
Designer Project
The designer version of the control project has a special Conditional Compilation
Constant named 'NETCFDESIGNTIME'. This constant is used to add extra code for design-time
support. Attributes are added to methods. Extra classes and methods are included
for resizing, serialization and type conversion. The output of this project should
be copied to the C:\program files\microsoft visual studio .net 2003\compactframeworksdk\v1.0.5000\Windows
CE\Designer folder.
#if NETCFDESIGNTIME
[
Category("Appearance"),
Description("The current time
of the timebox."),
]
#endif
public ReflectionIT.Time
Time {
get
{
return
(this._time);
}
set
{
bool
changed = (_time
!= value);
this._time
= value;
if
(changed)
{
Invalidate();
OnTimeChanged();
}
}
}
#if NETCFDESIGNTIME
public void ResetTime()
{
this.Time
= new Time(0);
}
public bool ShouldSerializeTime()
{
return this.Time !=
new Time(0);
}
#endif
Sample Client Project
The TimeBox Sample client project has two TimeBox controls and a disabled TextBox
control. The difference between the two times is displayed in the TextBox using
the TimeChanged event of the TimeBoxes.
private void
timeBox_TimeChanged(object
sender, System.EventArgs e)
{
if (timeBoxStop.Time
>= timeBoxStart.Time)
{
textBoxDuration.Text =
(timeBoxStop.Time
- timeBoxStart.Time).ToString();
} else
{
textBoxDuration.Text =
new ReflectionIT.Time().ToString();
}
}
Links:
I have used the following articles to create this control and to write this article:
Conclusion
Writing this TimeBox control was realy a challenge. The documentation of the .NetCF
is very limited. I hope that this article and sample code will help you write your
own controls for the Pocket PC.
I hope I can port these applications to a Smartphone solution as soon as Microsoft
will produce a .NET Compact Framework for it. A Pocket PC is just to big to carry
around the whole day. Hopefully it works!
Any suggestions and feedback for improving this article is most welcome. Send your
suggestions and feedback to Fons.Sonnemans@reflectionit.nl