Cały czas w C# brakowało mi porządnego pola numerycznego. Jakoś w clipperze moglo byc pomimo tego, ze w clipperze niczego nie bylo a w C# nie ma choć jest wszystko ;-)
No wiec napisalem sobie wreszcie obsluge takiego pola.
Problem w tym, ze chcac uniknac kazdorazowego wklejania kodu (badz wywolywania metod) do eventsow przydalby sie wstepnie zdefiniowany taki TextBox. Tyle, ze nie wiem jak się do tego zabrac.
Częściowo wykorzystałem kod z netu. Działa to tak:
-
można wprowadzic tylko cyfry, przecinek i minus
-
przecinek i minus moga wystapic tylko raz
-
minus tylko na poczatku
-
po wyjsciu z pola wartosc jest zaokraglana do 2 miejsc po przecinku
-
po wyjsciu sa wstawiane seperatory tysiecy
-
przy wejsciu seperatory tysiecy sa usuwane
Poniżej kod obsługujący pole numeryczne:
using System.Globalization;
using System.Text.RegularExpressions;private void NominalTextBox_KeyPress(object sender, KeyPressEventArgs e) { string decimalSeparator = numberFormatInfo.NumberDecimalSeparator; string negativeSign = numberFormatInfo.NegativeSign; string keyInput = e.KeyChar.ToString(); if (Char.IsDigit(e.KeyChar)) { // digit is OK } else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(negativeSign)) { // Decimal separator is OK if (keyInput.Equals(decimalSeparator) & ((TextBox)sender).Text.IndexOf(decimalSeparator) != (-1)) { e.Handled = true; // przecinek moze wystapic tylko raz } else if (keyInput.Equals(negativeSign) & ((TextBox)sender).Text.IndexOf(negativeSign) != (-1)) { e.Handled = true; // minus moze wystapic tylko raz } else if (keyInput.Equals(negativeSign) & ((TextBox)sender).SelectionStart != 0) { e.Handled = true; // minus moze byc tylko na poczatku } } else if (e.KeyChar == '\b') { // Backspace key is OK } // else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0) // { // // Let the edit control handle control and alt key combinations // } else { // Consume this invalid key and beep e.Handled = true; // MessageBeep(); } } private void NominalTextBox_Leave(object sender, EventArgs e) { string decimalSeparator = numberFormatInfo.NumberDecimalSeparator; string groupSeparator = numberFormatInfo.NumberGroupSeparator; ((TextBox)sender).Text = decimal.Round(decimal.Parse(((TextBox)sender).Text), 2).ToString(); int pozycjaPrzecinka = ((TextBox)sender).Text.IndexOf(decimalSeparator); if (pozycjaPrzecinka==(-1)) ((TextBox)sender).Text = ((TextBox)sender).Text + decimalSeparator + "00"; else if ((((TextBox)sender).Text.Length) - pozycjaPrzecinka == 2) ((TextBox)sender).Text = ((TextBox)sender).Text + "0"; string numberMask=""; int seperator = 6; for (int i = 0; i <= ((TextBox)sender).Text.Length - 1; i++) { numberMask = ((TextBox)sender).Text.Substring(((TextBox)sender).Text.Length - i - 1, 1) + numberMask; if (numberMask.Length == seperator) { numberMask = groupSeparator + numberMask; seperator = seperator + 4; } } ((TextBox)sender).Text = numberMask.Trim(); } private void NominalTextBox_Enter(object sender, EventArgs e) { ((TextBox)sender).Text=Regex.Replace(((TextBox)sender).Text, @"\s", ""); }
</ol>