|
-
Dec 16th, 2011, 07:22 AM
#1
Thread Starter
Hyperactive Member
Controls at Runtime
Ladies, gents and everything in between. I've been searching far and wide across the lands to find a way to create an array of controls at runtime. I basically have a user enter text in a text box, click on the form and the text then appears in the form of a label at the cursor location.
However,
I can't work out how to create an array of controls so that I can manage them, iterate through them etc when required. Is there a simple way to create a control array without using complicated API's or workarounds?
(I also require this so that I can have click events for each individual label)
Cheers,
Jord
-
Dec 16th, 2011, 07:43 AM
#2
Re: Controls at Runtime
here's how. this creates an array of labels + an array of textboxes from existing controls:
vb Code:
Public Class Form1
Dim labels() As Label
Dim textboxes() As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
labels = New Label() {Label1, Label2, Label3}
textboxes = New TextBox() {TextBox1, TextBox2, TextBox3}
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 16th, 2011, 07:43 AM
#3
Re: Controls at Runtime
This code will get all of the controls. Based on your requirements I don't see how this helps.
Code:
Dim allControls As New Dictionary(Of String, Control)
Private Sub GetAllControls()
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
allControls.Add(ctrl.Name, ctrl)
ctrl = Me.GetNextControl(ctrl, True)
Loop
End Sub
You can have click events for labels without it.
-
Dec 16th, 2011, 08:27 AM
#4
Thread Starter
Hyperactive Member
Re: Controls at Runtime
 Originally Posted by .paul.
here's how. this creates an array of labels + an array of textboxes from existing controls:
vb Code:
Public Class Form1 Dim labels() As Label Dim textboxes() As TextBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load labels = New Label() {Label1, Label2, Label3} textboxes = New TextBox() {TextBox1, TextBox2, TextBox3} End Sub End Class
Thanks for this pointer. The difficulty is that the controls are created at runtime, they don't already exist.
Code:
Dim newlab As New Label
newlab.Text = textBxad.Text
newlab.Left = Cursor.Position.X - 15
newlab.Top = Cursor.Position.Y - 118
newlab.AutoSize = True
textBxad.Text = ""
groupbox.Controls.Add(newlab)
I'd like to have an index through which I can iterate. i.e newlab(1), newlabl(2) etc. The code I have quoted works fine but I assume it just creates a new "newlab" label and forgets about the old one each time it runs resulting in the old label being inaccessible.
-
Dec 16th, 2011, 08:37 AM
#5
Re: Controls at Runtime
So create them in a loop
Code:
'assumes GroupBox is created
For x As Integer = 1 To 10
Dim newlab As New Label
newlab.Name = "myLbl" & x.ToString
newlab.Text = textBxad.Text
newlab.Left = Cursor.Position.X - 15
newlab.Top = Cursor.Position.Y - 118
newlab.AutoSize = True
textBxad.Text = ""
AddHandler newlab.Click, AddressOf fooLabel_Click 'some handler if needed
GroupBox.Controls.Add(newlab)
Next x
-
Dec 16th, 2011, 11:03 AM
#6
Re: Controls at Runtime
I agree. If you are creating the controls, then you get to say where they are located. While you have to put them into the proper controls collection, you can also put them anywhere else that you want. In your case, you also want them in some kind of a collection, perhaps a list, or possibly a Dictionary such that you can access them by name.
My usual boring signature: Nothing
 
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
|