-
Oct 15th, 2016, 11:18 AM
#1
Thread Starter
Junior Member
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.
-
Oct 15th, 2016, 05:37 PM
#2
Thread Starter
Junior Member
Re: Find only the controls i want on a form
Ok guys after some tinkering i got it working the way i want it. Almost. The revised code at the bottom of this post gets the correct list, filtered by type of control. I have made the comparison between the type of the current control and the type to be filtered by using the TypeName function.
However this is relatively slow, so i have another question about what is the best way to pass the type of control to filter to GetAllControls, and this has been driving me bonkers!
I tried, unsuccessfully, to do the following. (Relevant text highlighted in bold.) I spent a LONG time trying different things and could not get the evaluation to work. I checked with watches, and found that both the DepChild and ctrl referred to a textbox.
But one of them evaluated to "type of Checkbox", the other to "Checkbox". I could not get any kind of comparison between the two to evaluate to true.
So how do i make this work? Even though i found another solution, i want to know!
Code:
Dim listofControls As List(Of DependencyObject) = GetAllControls(form, TypeOf(TextBox))
...
Private Shared Function GetAllControls(ByVal parent As DependencyObject, Optional ByVal ctrl As Type = Nothing) As List(Of DependencyObject)
Dim children As IEnumerable
children = LogicalTreeHelper.GetChildren(parent)
For Each child In children
If (TypeOf (child) Is DependencyObject) Then
Dim depChild As DependencyObject = CType(child, DependencyObject)
If (ctrl Is Nothing OrElse typeof(DepChild) is ctrl) Then
listofControls.Add(depChild)
.....
(Below is the latest, -working- version of the function, just for completeness)
Code:
Option Strict On
Public partial Class Utilities
Public Shared Sub ResetAllControls(ByVal form As Control)
Dim listofControls As List(Of DependencyObject) = GetAllControls(form, New TextBox)
For i As Integer = 0 To listofControls.Count - 1
Dim item As DependencyObject = listofControls(i)
If (TypeOf (item) Is TextBox) Then
Dim tb As TextBox = CType(item, TextBox)
tb.Text = ""
End If
Next
End Sub
Private Shared Function GetAllControls(ByVal parent As DependencyObject, Optional ByVal ctrl As Control = Nothing) As List(Of DependencyObject)
Dim listofControls As New List(Of DependencyObject)
Try
If (parent Is Nothing) Then
Throw New ArgumentNullException(parent.ToString, "Argument of GetAllControls was NULL")
End If
Dim children As IEnumerable
children = LogicalTreeHelper.GetChildren(parent)
For Each child In children
If (TypeOf (child) Is DependencyObject) Then
Dim depChild As DependencyObject = CType(child, DependencyObject)
If (ctrl Is Nothing OrElse TypeName(depChild).Equals(TypeName(ctrl))) Then
listofControls.Add(depChild)
End If
listofControls.AddRange(GetAllControls(depChild, ctrl))
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 06:32 PM.
Reason: Removed a superfluous check
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|