Adding Controls at Runtime
I need a little help with variable scope ( I think that is the right term)
As I understand it, the follow could be used to add a button to my form at runtime ...
Dim newButton As Button
newButton = New Button()
newButton.Location = ... etc...
I think this defines a new object of type Button() called "newButton".
Here is the quesiton ... Can "newButton" be a pointer to an object name?
I want a method that creates a new button with the object name that I pass to the method.
I am not sure how to do this.
Thanks,
David
Re: Adding Controls at Runtime
'newButton' is a reference to a Button object. That's what it is and that's all it can be. That Button object has a Name property that you can set and you can then use that name to retrieve a reference to that Button from the Controls collection of its Parent. For instance, if you do this:
vb.net Code:
Dim btn As New Button
btn.Name = "Clicker"
Me.Controls.Add(btn)
then you can later do this:
vb.net Code:
Dim btn As Button = DirectCast(Me.Controls("Clicker"), Button)
If that doesn't provide the information you need then you'll need to provide a clearer explanation of what you're trying to to achieve because this:
Quote:
Can "newButton" be a pointer to an object name?
doesn't really make sense. There basically aren't any pointers in VB and, in languages that do support them, you would have a pointer to an object, not a pointer to the name of an object