Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to set focus on dynamically created controls?

How to set focus on dynamically created controls?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Floodlight
    wrote on last edited by
    #1

    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!

    _ 1 Reply Last reply
    0
    • F Floodlight

      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!

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #2

      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.

      F 1 Reply Last reply
      0
      • _ _Erik_

        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.

        F Offline
        F Offline
        Floodlight
        wrote on last edited by
        #3

        Thanks heaps for that Erik, it worked!! Top job :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups