[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?
Re: Referring to a control which created programmatically
Quote:
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.
Re: Referring to a control which created programmatically
Thanks a lot, I also learned .Find here.
Quote:
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.
Re: Referring to a control which created programmatically
Quote:
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.
Re: Referring to a control which created programmatically
Quote:
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.
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
Quote:
.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?
Re: Referring to a control which created programmatically
Quote:
Originally Posted by
dbasnett
This feels like one of those questions that will keep wandering around.
Sorry, not intended to do such thing.
Quote:
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.