Problem with cancellation (returning watermark) non-numeric characters after pressing Ctrl + Z or Ctrl + Y in Text Box, in WPF (by C#)

رضا جافری 1,291 Reputation points
2022-02-04T20:52:19.443+00:00

First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I created a watermark for each of the Text Boxes in my project using the following method:
For example:

    Language_TextBox.Foreground = Brushes.Silver;  
    Language_TextBox.Text = "Language";  
    private void Language_TextBox_GotFocus(object sender, RoutedEventArgs e)  
    {  
        try  
        {  
            switch ((Language_TextBox.Text == "Language") && Language_TextBox.Foreground == Brushes.Silver)  
            {  
                case true:  
                    Language_TextBox.Text = "";  
                    Language_TextBox.Foreground = Brushes.Black;  
                    break;  
            }  
        }  
        catch  
        {  
        }  
    }  

    private void Language_TextBox_LostFocus(object sender, RoutedEventArgs e)  
    {  
        try  
        {  
            switch (Language_TextBox.Text == "" || Language_TextBox.Foreground == Brushes.Silver)  
            {  
                case true:  
                    Language_TextBox.Foreground = Brushes.Silver;  
                    Language_TextBox.Text = "Language";  
                    break;  
            }  
        }  
        catch  
        {  
        }  
    }  

Some Text Boxes are numeric Text Boxes, so I created a filter for their input using the following method:

    private void BuyerPhoneNumber_TextBox_PreviewKeyDown(object sender, KeyEventArgs e)  
    {  
        switch (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Back || e.Key == Key.Home || e.Key == Key.End || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Tab || Keyboard.Modifiers == ModifierKeys.Control))  
        {  
            case true:  
                e.Handled = true;  
                break;  
            default:  
                switch (e.Key == Key.A || e.Key == Key.C || e.Key == Key.V || e.Key == Key.Y || e.Key == Key.Z)  
                {  
                    case true:  
                        break;  
                }  
                break;  
        }  
    }  

But when I press Ctrl + Z or Ctrl + Y on the numeric Text Box, the watermark text (black watermark) is displayed in the Text Box:
171487-watermark.gif
The problem will be fixed if the watermark text color returns to the default (silver) state.

Thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,698 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,512 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
778 questions
{count} votes

Accepted answer
  1. Viorel 114K Reputation points
    2022-02-04T21:47:16.673+00:00

    Try adding these lines after BuyerPhoneNumber.Text = "":

    BuyerPhoneNumber.UndoLimit = 0;
    BuyerPhoneNumber.UndoLimit = -1;
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful