Showing posts with label .Net CF. Show all posts
Showing posts with label .Net CF. Show all posts

Tuesday, December 15, 2009

C# Compact Framework: Numeric TextBox

I was recently in need of a numeric textbox for a small compact framework application I’m writing at home. The best I could find on the web was this MSDN article, but unfortunately it wasn’t as bullet proof as I’d like it to be, so I used the code as a base, and wrote my own.

Features:

  • Allows numeric only entry
  • DecimalPlaces property
    • Specify how many decimal places the number should have. 0 decimal places = integer numbers (no decimal separator allowed)
    • Defaults to 2 decimal places
  • AllowNegatives property
    • Specifies whether to allow negative number values or not
    • When the number is negative, the font changes color from black to red
    • defaults to not allow negative number entries

Sample application with code:

The code isn’t exactly pretty, but it works, and that’s all I was after.

Feel free to take it for a spin, and let me know if you can break it, so I can fix any loopholes.

Friday, November 9, 2007

How To: Play Sounds (C# 2.0 Compact Framework)

NOTE: If you're targeting the 3.5 framework, you can use the new SoundPlayer class

I'm busy writing a simple Egg Timer Application in my spare time for
my Mobile Phone (I keep forgetting to switch the damn water off when
watering the plants / filling the pool!)
One of the things I needed to do was to play some sound when the time
ran out. The sound file also needed to be embedded in the application
so as to avoid dependencies (which tend to cause trouble).

I found this article from M$.
I had specific problem with this:
Sound sound = new Sound(Assembly.GetExecutingAssembly().GetManifestResourceStream("SoundSample.chimes.wav"));
sound.Play();
I spent quite a bit of time trying to get the sound loaded, but to no avail.

Here's the work-around:
1) Add a sound file as a resource:








- Open the Resources.resx file of the project, and add an Audio Resource
- Select "Audio" type, and click "Add Resource"
- Browse to the sound file you would like to use (*.wav)

2) Copy the Sound class from the above M$ article, but change the constructor that takes a Stream, and change it to a byte[] parameter. Like so:
public Sound(byte[] soundBytes)
{
m_soundBytes = soundBytes;
}

3) Then instead of using GetManifestResourceStream(), pass sound bytes into the constructor if the Sound class:
eg:
Sound sound = new Sound(JaxEggTimer.Properties.Resources.beep);
sound.Play();

If anyone has managed to get the GetManifestResourceStream thing to work, please let me know how. (Leave a comment for others to see)