Results 1 to 7 of 7

Thread: [RESOLVED] Referring to a control which created programmatically

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    347

    Resolved [RESOLVED] Referring to a control which created programmatically

    Consider you've created a control inside a form as follows:
    Code:
    Me.Controls.Add(New Label With {.Name = "Label1"})
    How can I access it right after creation line accordingly?
    Code:
    Label1.Visible = True
    Its not declaration error will be shown consequently.
    How about declaring controls with similar names? Required argument accepts string after all and humans can make mistakes.

    How about adding controls inside a TableLayoutPanel? Are the logic dictation applies here too?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,307

    Re: Referring to a control which created programmatically

    Quote Originally Posted by pourkascheff View Post
    Consider you've created a control inside a form as follows:
    Code:
    Me.Controls.Add(New Label With {.Name = "Label1"})
    How can I access it right after creation line accordingly?
    Code:
    Label1.Visible = True
    Its not declaration error will be shown consequently.
    How about declaring controls with similar names? Required argument accepts string after all and humans can make mistakes.

    How about adding controls inside a TableLayoutPanel? Are the logic dictation applies here too?
    Giving a control a name at runtime doesn't automatically create a variable of the same name. One way would be to create the variable explicitly and then add it to the collection e.g.
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim l1 As New Label With {
            .Name = "Label1"
        }
    
        Me.Controls.Add(l1)
        l1.Visible = True
    End Sub
    alternatively you could use the Find method on the Controls collection to locate the new Label and use that e.g.
    Code:
    Me.Controls.Add(New Label With {.Name = "Label1"})
    
    Dim l1 As Label = DirectCast(Me.Controls.Find("Label1", True).First(), Label)
    l1.Visible = True
    another option would be to just set all the properties in one go when you create the control e.g.
    Code:
    Me.Controls.Add(New Label With {
        .Name = "Label1",
        .Visible = True
        } )
    obviously that isn't any good if you do need to do other things with the control.

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    347

    Re: Referring to a control which created programmatically

    Thanks a lot, I also learned .Find here.
    Quote Originally Posted by PlausiblyDamp View Post
    that isn't any good if you do need to do other things with the control.
    Consider you're arranging a huge matrix in TLP and you want to do things with its members (controls) they could be UserControl and .Visible was just an example. About TLP .GetControlFromPosition(x, y) would be our friend then.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,307

    Re: Referring to a control which created programmatically

    Quote Originally Posted by pourkascheff View Post
    Thanks a lot, I also learned .Find here.

    Consider you're arranging a huge matrix in TLP and you want to do things with its members (controls) they could be UserControl and .Visible was just an example. About TLP .GetControlFromPosition(x, y) would be our friend then.
    .GetControlFromPosition would probably be fine in that case, the only possible issue would be knowing what kind of control you are dealing with; however if the TLP has a predictable structure e.g. column 1 is always a particular control type then this would be pretty straightforward to do.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,687

    Re: Referring to a control which created programmatically

    Quote Originally Posted by pourkascheff View Post
    Thanks a lot, I also learned .Find here.

    Consider you're arranging a huge matrix in TLP and you want to do things with its members (controls) they could be UserControl and .Visible was just an example. About TLP .GetControlFromPosition(x, y) would be our friend then.
    Maybe if you told us EXACTLY what you are trying to accomplish... This feels like one of those questions that will keep wandering around.

    edit: Make hint more pronounced.
    Last edited by dbasnett; Sep 22nd, 2023 at 01:50 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Referring to a control which created programmatically

    If you know the name, type, and parent of control you dont need to find it
    Code:
    Controls.Add(New Label With {.Name = "Label1"})
    DirectCast(Controls("Label1"), Label).Visible = True
    .GetControlFromPosition(x, y) would be our friend then
    You lost me here, are you suggesting that it would be easier to reference a control by its location than it would be by its name and/or type?
    Last edited by vbdotnut; Sep 22nd, 2023 at 04:50 AM.

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    347

    Re: Referring to a control which created programmatically

    Quote Originally Posted by dbasnett View Post
    This feels like one of those questions that will keep wandering around.
    Sorry, not intended to do such thing.

    Quote Originally Posted by vbdotnut View Post
    are you suggesting that it would be easier to reference a control by its location than it would be by its name and/or type?
    Nah mate, getcontrolfromposition is useful when you are about to change something in a specific coordinate. But thanks for directcast anyways.

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