Results 1 to 2 of 2

Thread: Find only the controls i want on a form

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    17

    Question Find only the controls i want on a form

    Hi,

    i'm trying to write some code to locate and clear the textboxes on a WPF form. However i'm trying to do so from outside the class, so i can reuse the code later. The problem is that at this time it's not working very well. For example: if I only add controls to the list with no children, i get the textblock inside the button instead of the button. I only want the controls as i place them on the form, so i can reset them if needed.
    Now i have two questions:
    1- Should i just filter on the type of the control to pick what goes on the list, or is there a better way of doing it?
    2- I ended up with a list<of DependencyObject>. Each element of this list contained a control. Can i address the properties of the control in the list directly, or do i first need to convert the item back to a control?

    Code:
    Option Strict On
    Public Class Utilities
        Public Shared Sub ResetAllControls(ByVal form As Control)
            Dim listofControls As List(Of DependencyObject) = GetAllControls(form)
            For i As Integer = 0 To listofControls.Count - 1
                Dim v As DependencyObject = listofControls(i)
                If (TypeOf (v) Is TextBox) Then
                    Dim tb As TextBox = CType(v, TextBox)
                    tb.Text = ""
                End If
            Next
        End Sub
    
        Private Shared Function GetAllControls(ByVal parentcontrol As DependencyObject) As List(Of DependencyObject)
            Dim listofControls As New List(Of DependencyObject)
            Try
                If (parentcontrol Is Nothing) Then
                    Throw New ArgumentNullException(parentcontrol.ToString, "Argument of GetAllControls was NULL")
                End If
                Dim i, count As Integer
    
                count = VisualTreeHelper.GetChildrenCount(parentcontrol)
                For i = 0 To count - 1
                    Dim child As DependencyObject = VisualTreeHelper.GetChild(parentcontrol, i)
    
                    'if (TypeOf(child) is TextBox) then 'Should i just go with this???
                    If (CBool(VisualTreeHelper.GetChildrenCount(child))) Then
                        listofControls.AddRange(GetAllControls(child))
                    Else
                        listofControls.Add(child)
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Return listofControls
        End Function
    End Class
    Last edited by F Scheltens; Oct 15th, 2016 at 11:32 AM. Reason: Editted the code to remove the Visual Class, as it really did not add anything.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width