Ahh, I see what you are trying to do.
Well, it looks more like you would want to use an array- or, more precisely (and as I noted earlier) a dictionary. After all, it would be utterly pointless to have the variable name not known until run-time, because the code that is supposed to use and manipulate that variable are written far earlier.
Here is one approach to dynamically adding Buttons; I use a dictionary.
-Create a Form (just stick with Form1) in a new test app; Create a textbox and a button (use the default names), ideally laid out near the top, with some vertical space underneath.
-Paste the following into the Declarations Section:
Dim CurrentYPos As Integer
Dim LoadedButtons As Dictionary(Of String, Button) = New Dictionary(Of String, Button)()
The "CurrentYPos" variable is used for purely aesthetic purposes; LoadedButtons is used to keep track of all the buttons loaded so far. Note that it is "indexed" using the name the user specified.
In the Button1 Click event, paste this:
'get the name to use from the string.
Dim newbuttonname As String = TextBox1.Text
If newbuttonname = "" Then
MessageBox.Show("You must specify a Name.")
ElseIf LoadedButtons.Contains(newbuttonname) Then
MessageBox.Show("Name Already Exists.")
Else
'Create the button
Dim createdButton = New Button()
createdButton.Name = newbuttonname
createdButton.Text = "Button named " + newbuttonname
createdButton.Location = New Point(TextBox1.Left, CurrentYPos)
createdButton.Size = Button1.Size
createdButton.Width = TextWidth(createdButton.Text, createdButton.Font) * 2
CurrentYPos += createdButton.Height
'use "CreatedClick" for it's event handler
AddHandler createdButton.Click, AddressOf CreatedClick
Controls.Add(createdButton)
LoadedButtons.Add(newbuttonname, createdButton)
End If
Control
the Load event of the Form requires this, otherwise it starts the buttons at the top of the form:
[code]
CurrentYPos = TextBox1.Bottom + 5
And lastly, paste this at the bottom of the code listing:
Private Function TextWidth(ByVal measurestring As String, ByVal useFont As Font) As Integer
Dim usebitmap As Bitmap = New Bitmap(1, 1)
Dim Useg As Graphics = Graphics.FromImage(usebitmap)
Return Useg.MeasureString(measurestring, useFont).Width
End Function
Private Sub CreatedClick(ByVal Sender As Object, ByVal Args As EventArgs)
Dim gotcontrol As Control = Sender
MessageBox.Show("You clicked on " + gotcontrol.Name)
End Sub
One notable portion is the use of "AddHandler" to link <ALL> of the buttons to the same Event procedure. I feel that this may in many ways be the sort of thing you need (I'm guessing you want to handle the events of controls you add but the only way you were aware of was the use of the Handles clause on the end of a Sub definition, but that needs a control name, thus the requirement to have "user-defined" control names, etc).
After that, it's added to the Controls collection of the form, and then to the LoadedButtons Dictionary. The latter is purely for Access purposes- without it, it would be difficult to reference the controls outside their event procedures (the AddClick routine) since they wouldn't really have any Form-level reference. (note that controls that you add to a form aren't made implicitly available by the form as "properties" like those you add at design time; the Form Designer creates boilerplate code that adds properties for the controls you add at design time, but no such addition is done at run-time, so you have to do that management yourself.
Using this method, you can also access a specific control using the "name": that the user gave it, via the dictionary:
String choosebuttonname="Whatever"
If LoadedButtons.ContainsKey(choosebuttonname) Then
Button chosenbutton = LoadedButtons[choosebuttonname].Value
Else
MessageBox.Show("Control name not found.")
End If
I hope this helps.