When the decimal point is entered it disappears and the cursor moves back to the beginning of the textbox. RRS feed

  • Question

  • I have an app with multiple pages. At first i had no problem and the code worked as intended but after i implemented language resources i started getting error with the decimal. The thing is the code is still working perfect on one page but on the others the decimal disappears and cursor moves back to the beginning of the textbox. I tried to handle it on Keydown event but still got the same problem. The codes and textbox properties on different pages are exactly the same.

    private void validateTextDouble(object sender) { Exception X = new Exception(); TextBox T = (TextBox)sender; try { double x = double.Parse(T.Text); //Customizing Condition (Only numbers larger than or //equal to zero are permitted) if (x < 0 || T.Text.Contains(',')) throw X; } catch (Exception) { try { int CursorIndex = T.SelectionStart - 1; T.Text = T.Text.Remove(CursorIndex, 1); //Align Cursor to same index T.SelectionStart = CursorIndex; T.SelectionLength = 0; } catch (Exception) { } } private async void txtDemo_TextChanged(object sender, TextChangedEventArgs e) { if (txtDemo.Text != string.Empty) { validateTextDouble((object)txtDemo); double value; string val = await Clipboard.GetContent().GetTextAsync(); if (double.TryParse(val, out value)) { txtDemo.Text = val; }

    if (double.TryParse(txtDemo.Text, out demo))
                            demo= Convert.ToDouble(txtDemo.Text);

    txtDemo.Text = demo.ToString();

    } } }

    <TextBox x:Name="txtDemo" TextAlignment="Right" HorizontalAlignment="Right" Margin="0,3,0,0" VerticalAlignment="Top" Width="187" MaxLength="20" HorizontalContentAlignment="Right" InputScope="NumberFullWidth" TextWrapping="Wrap" GotFocus="txtDemo_GotFocus" TextChanged="txtDemo_TextChanged"/>

    Friday, February 20, 2015 8:07 PM


All replies

  • That's strange. Have you tested this with TextBox and a regular binding? Perhaps it's related to the TextChanged event firing to update the binding?

    Vote if help you

    Monday, February 23, 2015 10:49 AM
  • Thanks for the reply. It seems i was assigning the txtDemo.Text in the code again when i shouldn't since it is the input.
    Monday, February 23, 2015 2:05 PM
  • Yes, it is the point.

    Vote if help you

    Thursday, March 5, 2015 2:36 AM