question

BrandonBoone-0673 avatar image
0 Votes"
BrandonBoone-0673 asked BrandonBoone-0673 answered

on get double from key down

How do I only get a double from a key down ?
so if the user type 4.56
I want to display 4.56
but if they type 4.35 then backspace
I want 4.3
and
not 4.36Back

here is my code:



 if (e.Key == Key.Enter || e.Key == Key.Return) return;
                 string letter;
                 if (e.Key == Key.OemPeriod)
                     letter = ".";
                 else
                     letter  = e.Key.ToString();
        
                 if (letter.Contains("D"))
                     letter = Regex.Replace(letter, "D", String.Empty);
                 SliderTxt.Text = SliderTxt.Text + letter;


dotnet-csharpdotnet-wpf-xaml
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@BrandonBoone-0673 . Welcome Microsoft Q&A. Could you show the complete code relevant to your question? And what is your purpose? Can you try the method below to see if it works for you?
Xaml:

 <TextBox x:Name="tb" Text="{Binding Path=TransactionAmount,   Mode=TwoWay,   UpdateSourceTrigger=PropertyChanged}" 
                  Width="200" Height="105"  />

Codebehind:

 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Windows;
    
 public partial class MainWindow : Window, INotifyPropertyChanged
   {
      
     public MainWindow()
     {
       InitializeComponent();
     }
     private double _transactionAmount;
     public double TransactionAmount
     {
       get { return _transactionAmount; }
       set
       {
         if (_transactionAmount != value)
         {
           _transactionAmount = value;
           OnPropertyChanged();
         }
       }
    
     }
     public event PropertyChangedEventHandler PropertyChanged;
     protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }

The result:

214686-image.png

0 Votes 0 ·
image.png (3.4 KiB)
HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered BrandonBoone-0673 commented

Or do you want something like the following?
Xaml:

  <TextBox x:Name="tb2"   Width="200" Height="105"  PreviewTextInput="txt_PreviewTextInput"/>

Codebehind:

  private void txt_PreviewTextInput(object sender, TextCompositionEventArgs e)
     { 
      
       foreach (char ch in e.Text)
         if (!(Char.IsDigit(ch) || ch.Equals('.')))
         {
           e.Handled = true;
         }
     }


If the response is helpful, please click "Accept Answer" and upvote it.
 Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrandonBoone-0673 avatar image
0 Votes"
BrandonBoone-0673 answered

All
thanks for the help but I got it to work now.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.