Search code examples
windows-phone-7windows-phone-7.1

Can't use Decimal values


I've created a TextBox in which currency values have to be entered but now I am not able to enter .(dot) in the textbox for decimal values. I have written the code in Text_changed event. The code

 int cursorLocation = ((TextBox)(sender)).SelectionStart;

            string inputval = ((TextBox)sender).Text;
            inputval = inputval.Replace("*", "");
            inputval = inputval.Replace("#", "");
            inputval = inputval.Replace(",", "");
            inputval = inputval.Replace(" ", "");
            inputval = inputval.Replace("-", "");
            Decimal inputval1 = Convert.ToDecimal(inputval);

            ((TextBox)sender).Text = inputval1.ToString("###,###");
            cursorLocation = ((TextBox)(sender)).Text.Length;

            //((TextBox)sender).Text = inputval;
            ((TextBox)(sender)).SelectionStart = cursorLocation;

Solution

  • I just did this under textchanged event and this worked,

     try
            {
                string value = TextBox.Text.Replace(",", "");
                 long ul;
                 if (long.TryParse(value, out ul))
                 {
                     TextBox.TextChanged -= TextBox_TextChanged;
                     TextBox.Text = string.Format("{0:#,#}", ul);
                     TextBox.SelectionStart = TextBox.Text.Length;
                     TextBox.TextChanged += TextBox_TextChanged;
                     this.TextBox.MaxLength = 10;
                 }
            }
            catch { }