C#: Compact framework, numericonly TextBox

Voor een TextBox puur numeriek te maken is er de volgende simpele code in het .NET 2.0 framework:

InputModeEditor.SetInputMode(textBox, InputMode.Numeric);

Dan kunt ge nog steeds # en * invoeren, ik wou dat deze 2 veranderd werden in een comma, dus ik ben tot de volgende code gekomen:

Windows.Forms.TextBox textBox = new Windows.Forms.TextBox();
this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Numeric_KeyPress);

/* ... */
private void Numeric_KeyPress(object sender, KeyPressEventArgs e)
{
	char decimalSeperator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToCharArray()[0];

	if ((e.KeyChar == '#' || e.KeyChar == '*'))
	{
		if (((TextBox)sender).Text.IndexOf(decimalSeperator) == -1)
			((TextBox)sender).SelectedText += decimalSeperator;

		e.Handled = true;
	}
}

Enjoy :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>