The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Event of textbox "x" button RRS feed

  • Question

  • There is a clean ("x") button in the right side of textbox, is there any event listening about this button clicked?
    Wednesday, December 21, 2016 12:38 PM

Answers

  • The TextBox doesn't raise any specific event when the delete button is clicked but if you're not happy with handling the TextChanged event and want to be able to determine whether the button was actually clicked, you could handle the Loaded event of the TextBox, find the delete button in the visual tree and hook up an event handler to its Click event:

    <TextBox Loaded="TextBox_Loaded" />

    private void TextBox_Loaded(object sender, RoutedEventArgs e)
            {
                TextBox textBox = sender as TextBox;
                Button deleteButton = FindVisualChild<Button>(textBox);
                if(deleteButton != null)
                {
                    deleteButton.Click += async (ss, ee) => 
                    {
                        var dialog = new Windows.UI.Popups.MessageDialog("clicked...");
                        await dialog.ShowAsync();
                    };
                }
    
            }
    
            private static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            return (T)child;
                        }
    
                        T childItem = FindVisualChild<T>(child);
                        if (childItem != null) return childItem;
                    }
                }
                return null;
            }
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Breeze Liu Thursday, December 22, 2016 5:15 AM
    • Marked as answer by William A Wang Monday, January 2, 2017 10:01 AM
    Wednesday, December 21, 2016 7:34 PM

All replies

  • Hi!

    You have the same topic here : https://social.msdn.microsoft.com/Forums/windowsapps/en-US/0ae68b99-73e0-43a6-8ace-8b3178e88bab/textbox-clear-cross?forum=winappswithcsharp

    Hope it wil be useful.

    If it's good for you, can you mark this answer as answer of your question please?

    Follow me on twitter : https://twitter.com/NordineMhoumadi


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don&amp;amp;amp;amp;#39;t ask several questions in the same thread. SVP utiliser le Marquer comme reponse; sur les reponses qui vous aide car cela permet aux personnes qui peuvent rencontrer le meme probleme que vous, de retrouver rapidement quelle est la reponse qui a resolu leur probleme. De plus, cela encourage aussi la personne qui a repondu a votre question, et de repondre aux suivantes...

    Wednesday, December 21, 2016 12:51 PM
  • The TextBox doesn't raise any specific event when the delete button is clicked but if you're not happy with handling the TextChanged event and want to be able to determine whether the button was actually clicked, you could handle the Loaded event of the TextBox, find the delete button in the visual tree and hook up an event handler to its Click event:

    <TextBox Loaded="TextBox_Loaded" />

    private void TextBox_Loaded(object sender, RoutedEventArgs e)
            {
                TextBox textBox = sender as TextBox;
                Button deleteButton = FindVisualChild<Button>(textBox);
                if(deleteButton != null)
                {
                    deleteButton.Click += async (ss, ee) => 
                    {
                        var dialog = new Windows.UI.Popups.MessageDialog("clicked...");
                        await dialog.ShowAsync();
                    };
                }
    
            }
    
            private static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            return (T)child;
                        }
    
                        T childItem = FindVisualChild<T>(child);
                        if (childItem != null) return childItem;
                    }
                }
                return null;
            }
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Breeze Liu Thursday, December 22, 2016 5:15 AM
    • Marked as answer by William A Wang Monday, January 2, 2017 10:01 AM
    Wednesday, December 21, 2016 7:34 PM
  • Hi William A Wang,

    Have your issue resolved? We are trying to give you the best answer for your issue.

    Best regards,
    Breeze Liu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 2, 2017 9:49 AM
  • The answer from Breeze is good, thanks for all of you.
    Monday, January 2, 2017 10:02 AM