January 28, 2009

Clear TextBoxes from a Container control in Silverlight




If you have been looking out for a 'Clear Text' kinda funtionality in Silverlight, use this code. This code clear's the text of the TextBox and TextBlock kept in a container control like the Grid, Canvas or StackPanel.

C#


    private void btnClear_Click(object sender, RoutedEventArgs e)

    {

        foreach (object child in container.Children)

        { 

            if (child is TextBlock || child is TextBox)

                (child as TextBlock).Text = String.Empty;

        }

    }



VB.NET


    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

        For Each child As Object In container.Children

            If TypeOf child Is TextBlock OrElse TypeOf child Is TextBox Then

                TryCast(child, TextBlock).Text = String.Empty

            End If

        Next child

    End Sub



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Clear TextBoxes from a Container control in Silverlight"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions