How to set focus on dynamically created controls?
-
Hi All, I have a program where I dynamically create some Textboxes within a Panel on my form using this code
Panel1.Controls.AddRange(New System.Windows.Forms.Control() {TextBoxes(num - 1)})
These textboxes are automatically labeled Layer1, Layer2 etc etc.. Everything works fine. I also have a listbox that contains the names of these dynamically created "Layers". What I want to do is when I click on the name in the listbox I want to "select" (or set the focus) on that particular dynamic control.. but I don't know how to reference it, as it's dynamic lol. Any help would be MUCH appreciated, thanks!
-
Hi All, I have a program where I dynamically create some Textboxes within a Panel on my form using this code
Panel1.Controls.AddRange(New System.Windows.Forms.Control() {TextBoxes(num - 1)})
These textboxes are automatically labeled Layer1, Layer2 etc etc.. Everything works fine. I also have a listbox that contains the names of these dynamically created "Layers". What I want to do is when I click on the name in the listbox I want to "select" (or set the focus) on that particular dynamic control.. but I don't know how to reference it, as it's dynamic lol. Any help would be MUCH appreciated, thanks!
-
Use its name as you have it:
Dim c as Control = Panel1.Controls[name];
Where "name" would be a string variable containing the name of the control. Edit: Sorry, my first reply was written in C#, not VB.
Thanks heaps for that Erik, it worked!! Top job :)