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


And shepherds we shall be, for thee my Lord for thee, power hath descended forth from thy hand, that our feet may swiftly carry out thy command. We shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine Patris, et Filii, et Spiritus Sancti.