-
Sep 21st, 2023, 05:02 AM
#1
Thread Starter
Hyperactive Member
[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?
-
Sep 21st, 2023, 05:20 AM
#2
Re: Referring to a control which created programmatically
 Originally Posted by pourkascheff
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.
-
Sep 21st, 2023, 07:34 AM
#3
Thread Starter
Hyperactive Member
Re: Referring to a control which created programmatically
Thanks a lot, I also learned .Find here.
 Originally Posted by PlausiblyDamp
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.
-
Sep 21st, 2023, 08:37 AM
#4
Re: Referring to a control which created programmatically
 Originally Posted by pourkascheff
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.
-
Sep 21st, 2023, 09:37 AM
#5
Re: Referring to a control which created programmatically
 Originally Posted by pourkascheff
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.
-
Sep 22nd, 2023, 04:45 AM
#6
Addicted Member
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.
-
Sep 22nd, 2023, 12:54 PM
#7
Thread Starter
Hyperactive Member
Re: Referring to a control which created programmatically
 Originally Posted by dbasnett
This feels like one of those questions that will keep wandering around.
Sorry, not intended to do such thing.
 Originally Posted by vbdotnut
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|