0

I have been searching for this but I am not able to find anything related to how to iterate through all dynamically created controls on the grid. I'm basically only looking to grab data from a dynamically created textbox, I have tried the following but it is not pulling up any children

Dim ctl As FrameworkElement = Me
Dim children As Integer = VisualTreeHelper.GetChildrenCount(ctl)

For i = 0 To children - 1
    Dim Child As FrameworkElement = VisualTreeHelper.GetChild(ctl, i)
Next

Code used to add a textbox onto the form

For i = 0 To tb - 1
    Dim t As New TextBox
    t.Width = 75
    t.Height = 25
    t.Focus()       
    Grid.SetColumn(t, columns)
    MyGrid.Children.Add(t)
     t.Focus()
Next
Criel
  • 805
  • 1
  • 14
  • 32
  • 1
    You don't "iterate" through "dynamically created controls" in WPF, because 1 - You don't "dynamically create controls", and 2, the UI is not the right place to do any iterations. See [My explanation](http://stackoverflow.com/questions/22794445/in-c-sharp-wpf-how-to-loop-through-a-grid-and-get-all-the-labels-inside-of-the-g/22794583#22794583) of the proper way to do what you're attempting here in WPF. – Federico Berasategui May 01 '14 at 15:41
  • Thanks, I understand that WPF we don't normally create our own controls through the code but in this case, I am creating a sort of custom window that appears that is dynamic based on what's passed to the constructor of the form, which is why I am creating these controls dynamically through code. I am not sure if there's a way to dynamically create them through xaml... – Criel May 01 '14 at 15:54
  • please read the linked explanation, it shows how to use an `ItemsControl` (or a derivative such as a `ListBox`) to dynamically create a set of UI elements in WPF corresponding to a collection of underlying data items. – Federico Berasategui May 01 '14 at 15:55
  • [here](http://stackoverflow.com/questions/23303985/taskfactory-new-ui-creation/23304651#23304651) is a similar example. – Federico Berasategui May 01 '14 at 15:59
  • This is amazing...I had no idea you could control UI elements without even needing to mess with the UI... You are a hero :) thank you!!!!!!!!!!! – Criel May 01 '14 at 16:57
  • Edit: I should really try to figure this stuff out before asking for help -.- – Criel May 01 '14 at 17:22
  • yes, either delete this question, or post an answer yourself and set that as accepted (probably linking to my original answers), but don't leave it open forever in limbo, let's keep StackOverflow as clean as we can – Federico Berasategui May 01 '14 at 18:24

0 Answers0