|
-
Feb 27th, 2008, 02:24 AM
#1
Thread Starter
Lively Member
How to add additional object to form through code
Hi.
I would like to know how to add additional objects like eg a textbox to a form through code for an event.
Thanks.
The God of Death Lives Again
-
Feb 27th, 2008, 02:45 AM
#2
Frenzied Member
Re: How to add additional object to form through code
Code:
Private Sub Form_Load()
Dim futureLabel As Label
Set futureLabel = Me.Controls.Add("VB.Label", "futureLabel")
futureLabel.Height = 300
futureLabel.Width = 550
futureLabel.Top = 250
futureLabel.Left = 250
futureLabel.Caption = "testing"
futureLabel.Visible = True
End Sub
-
Feb 27th, 2008, 03:36 AM
#3
Thread Starter
Lively Member
Re: How to add additional object to form through code
Hi Zack_vb6.
How would I be able to add these controls to say a picbox so that I can have them grouped and be able to scroll through them.
I dont have vb on this pc. I will have to try it out at home and let you know.
Thanks.
The God of Death Lives Again
-
Feb 27th, 2008, 04:56 AM
#4
Re: How to add additional object to form through code
set futurelabel.container = picturebox1
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 28th, 2008, 03:59 AM
#5
Thread Starter
Lively Member
Re: How to add additional object to form through code
Thanks Guys. Tried it at home and it works great.
Another question. How would you create a control array of a control through code?
The God of Death Lives Again
-
Feb 28th, 2008, 04:11 AM
#6
Re: How to add additional object to form through code
no can do, you can create all the controls you want at runtime, but control arrays have to be created at design time
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 28th, 2008, 08:05 AM
#7
Thread Starter
Lively Member
Re: How to add additional object to form through code
Ouch.
Then say I create Label1 to Label10 at runtime.
How can I reference the properties of the controls to simulate an array?
would it be something like: me.controls("Label" & i).caption="hello"
where i is a variable string?
The God of Death Lives Again
-
Feb 28th, 2008, 08:24 AM
#8
Re: How to add additional object to form through code
You would do something like
Code:
Private Sub lblLabel_Click(Index As Integer)
Select Case Index
Case 0
lblLabel(0).Caption = "Hello"
Case 1
lblLabel(1).Caption = "GoodBye"
'etc
End Select
End Sub
-
Feb 28th, 2008, 08:28 AM
#9
Re: How to add additional object to form through code
Actually, what you could do is create an array, then as you create the labels, add them to the array... then you should be able to access them through a pseudo control array (it would be an array of controls instead (or Label)..)
-tg
-
Feb 28th, 2008, 10:12 AM
#10
Thread Starter
Lively Member
Re: How to add additional object to form through code
Thanks Hack.
But how would I reference the different label names?
westconn1 said you cannot create a control array at runtime.
techgnome, how would I go about doing what you suggest?
dim arrLabel(0 to 10) as label
dim i as integer
for i=0 to 10
Set arrLabel(i) = Me.Controls.Add("VB.Label", "futureLabel" & i)
next
for i=0 to 10
arrLabel(i).caption="Hello" & i
next
The God of Death Lives Again
-
Feb 28th, 2008, 01:11 PM
#11
Re: How to add additional object to form through code
Why not put one label at design time on the form with Index = 0, then Load the rest of the labels at run time. That way you can get the events from the control also... while if you load a label/control using Me.Controls.Add() you won't get the events from the control.
-
Feb 29th, 2008, 01:52 AM
#12
Thread Starter
Lively Member
Re: How to add additional object to form through code
Basically this is what I want to use this for:
I got a db that is full of items. Now depending on the number of items in the db, for each item, I want to represent it with a label or shape. Now since at design time I have no idea of how many items I have in the db, I dont know how many shape controls to add to the form.
The God of Death Lives Again
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
|