question

Voytec avatar image
0 Votes"
Voytec asked NicoZhu-MSFT commented

UWP C# Number.Box key press.

Greetings dear M Community,

I have this code for hitting ENTER in a number box (from Microsoft.UI.Xaml).
And it has to be pressed twice until he gets wanted values - for the first time it equals a value: NaN.

Why I am facing such problem? What is the solution for this?

windows-uwp
· 7
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.

I can't reproduce your problem, could you share your code , Microsoft.UI.Xaml version and the app's target version and min version?

0 Votes 0 ·
  private void room_key_press(object sender, KeyRoutedEventArgs e) { if (e.Key == Windows.System.VirtualKey.Enter) { check(); } }
  private async void check()
          {
  ...
  StorageFolder folderSP = await KnownFolders.DocumentsLibrary.GetFolderAsync("Św. Puszcza App");
                      try { StorageFolder folderDATE = await folderSP.GetFolderAsync(date_string); }
                      catch { Windows.Storage.StorageFolder folderDATE = await folderSP.CreateFolderAsync(date_string, CreationCollisionOption.ReplaceExisting); }
                      StorageFolder newfolderDATE = await folderSP.GetFolderAsync(date_string);
                      try { StorageFile file = await newfolderDATE.GetFileAsync(room.Value + ".dat"); }
                      catch { Windows.Storage.StorageFile file = await newfolderDATE.CreateFileAsync(room.Value + ".dat", CreationCollisionOption.ReplaceExisting); }
                      Windows.Storage.StorageFile fileROOM = await newfoldeRDATE.GetFileAsync(room.Value + ".dat");
                      string text = await Windows.Storage.FileIO.ReadTextAsync(filerROOM);
  ...
        
  }

That was part of the code.

NuGet 2.7.0
Target 19041
Min 17763

@123-9099

0 Votes 0 ·

It looks the Number's default Enterkey effect your custom one, please try to set AcceptsExpression="False", and try again.

0 Votes 0 ·
Show more comments

1 Answer

NicoZhu-MSFT avatar image
1 Vote"
NicoZhu-MSFT answered NicoZhu-MSFT commented

Hello,
Welcome to Microsoft Q&A!

Please check the following code, and update your check method with double type parameter, and use newValue to replace Number Value property.



 private void NumberBox_KeyDown(object sender, KeyRoutedEventArgs e)
 {
     if (e.Key == Windows.System.VirtualKey.Enter)
     {
         Task.Run(() =>
         {           
             autoResetEvent.WaitOne();
             check(newValue);                  
            
         });
    
     }
 }
    
 private async void check(double roomvalue)
 {
    
     StorageFolder folderSP = await KnownFolders.DocumentsLibrary.GetFolderAsync("Św. Puszcza App");
     try { StorageFolder folderDATE = await folderSP.GetFolderAsync(date_string); }
     catch { Windows.Storage.StorageFolder folderDATE = await folderSP.CreateFolderAsync(date_string, CreationCollisionOption.ReplaceExisting); }
     StorageFolder newfolderDATE = await folderSP.GetFolderAsync(date_string);
     try { StorageFile file = await newfolderDATE.GetFileAsync(roomvalue + ".dat"); }
     catch { Windows.Storage.StorageFile file = await newfolderDATE.CreateFileAsync(roomvalue + ".dat", CreationCollisionOption.ReplaceExisting); }
     Windows.Storage.StorageFile fileROOM = await newfoldeRDATE.GetFileAsync(roomvalue + ".dat");
     string text = await Windows.Storage.FileIO.ReadTextAsync(filerROOM);
    
    
 }
 private double newValue;
 private double oldValue = -1;
 AutoResetEvent autoResetEvent = new AutoResetEvent(false);
 private void MyBox_ValueChanged(Microsoft.UI.Xaml.Controls.NumberBox sender, Microsoft.UI.Xaml.Controls.NumberBoxValueChangedEventArgs args)
 {
    
     if (args.NewValue != double.NaN)
     {
         newValue = args.NewValue;
         autoResetEvent.Set();
     }
    
 }

Xaml

<uicontrols:NumberBox 
    Height="44"
    KeyDown="NumberBox_KeyDown"          
    ValueChanged="MyBox_ValueChanged"
    Value="NaN" />


Thank you.


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.










· 7
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.

@NicoZhu-MSFT
142154-a.png



pokoj = room in English

0 Votes 0 ·
a.png (15.8 KiB)

What are the pokoj_vc and pokoj_key, I have not written, please replace them with above NumberBox_KeyDown and MyBox_ValueChanged event.

0 Votes 0 ·

still not working... :-(
@NicoZhu-MSFT

0 Votes 0 ·
Show more comments