r/visualbasic • u/Thunor_SixHammers • Feb 23 '19
Interacting with programmatically created items
I am fooling around with creating thing dynamically in code(buttons, text boxes, simple thing). So far I understand the very basics of creating them. For example for a combobox I do:
Dim combobox As New ComboBox
With combobox
.Location = New Size(20 * i, 20 * i)
.Name = "Combobox1"
.Items.Add("one")
End With
Me.Controls.Add(combobox)I put this on a button and it makes the combobox appears. My question is, after its been generated, how can I interact with it?
I have the .name property to label it as Combobox1, but when I try to add something outside of the With|EndWith box that refers to combobox1 I get an error that says it is undefined or unavailable due to protection level.
How can I interact with it once its been created, or is that not something thats done?
2
1
2
u/PostalElf VB.Net Intermediate Feb 23 '19
You'll need to maintain a reference to it, most likely in a form-level variable or collection. Alternatively, you'll have to trawl through Form.Controls and search for the item you want, most likely through its .name property.