|
-
Dec 22nd, 2006, 09:06 AM
#1
Thread Starter
Hyperactive Member
How to get a "click" in an component created at runtime ?
Hi, guys!
I'm in trouble with it...
I created an array of command buttons (for instance), at runtime, and I need to know which was clicked...
But when I click anyone I have no answer...
What's going on ?
Follows the code where I create the array of command Buttons:
VB Code:
Option Explicit
Dim lbl(4) As Label
Private Sub lbl1_Click()
MsgBox chkbox.Name
End Sub
Private Sub Form_Load()
Dim x As Byte
For x = 0 To 4
Set lbl(x) = Me.Controls.Add("VB.Label", "lbl" & Me.Controls.Count) '& "(" & x - 1 & ")")
With lbl(x)
.Left = 1000
.Top = 500 * x
.Width = 2000
.Height = 500
.Caption = lbl(x).Name
.Visible = True
End With
Next x
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
For i = 0 To 4
Set lbl(i) = Nothing
Next i
End Sub
tks!
João Luiz
-
Dec 22nd, 2006, 09:59 AM
#2
Re: How to get a "click" in an component created at runtime ?
You won't be able to. You can acchive that only with single control (not a control array) with the WithEvents:
VB Code:
Option Explicit
Private [B]WithEvents[/B] cmd As CommandButton
Private Sub Form_Load()
Set cmd = Me.Controls.Add("VB.CommandButton", "cmd")
With cmd
.Caption = "Click me!"
.Visible = True
End With
End Sub
Private Sub cmd_Click()
MsgBox "You clicked me!"
End Sub
If you want to have a control array then add one CommandButton at design time and change it's .Index property to 0. Then try this:
VB Code:
Option Explicit
Private Sub Form_Load()
Command1(Command1.ubound).Caption = Command1.ubound
loadCommandButtons [B]2[/B] 'will add 2 new CommandButtons...
End Sub
Private Sub loadCommandButtons(n As Integer)
For n = 1 To n
Load Command1(Command1.ubound + 1)
With Command1(Command1.ubound)
.Move Command1(Command1.ubound - 1).Left, Command1(Command1.ubound - 1).Top + Command1(Command1.ubound - 1).Height + 100, Command1(Command1.ubound - 1).Width, Command1(Command1.ubound - 1).Height
.Visible = True
.Caption = Command1.ubound
End With
Next n
End Sub
Private Sub Command1_Click(Index As Integer)
MsgBox Index
End Sub
-
Dec 22nd, 2006, 11:07 AM
#3
Thread Starter
Hyperactive Member
Re: How to get a "click" in an component created at runtime ?
Gavio,
Thanks your fast answer...
I tried the both ways you said...
My problem is:
If I use "WithEvents" I can't:
- create an array...
- change the component's name...
If I use "Set" I must to create a lot of controls, cos I have a "crazy" system, where I need to make severous arrays of controls...
Such this:
Label (0)
Label1 (0) check1(0)
Label1 (1) check1(1)
Label1 (2) check1(2)
Label (1)
Label2 (0) check2(0)
Label2 (1) check2(1)
Label2 (2) check2(2)
And I have no Idea how many label(X), Labelx(x) and checkx(x) I'll need at each time...
[]s
João Luiz
-
Dec 22nd, 2006, 11:29 AM
#4
Re: How to get a "click" in an component created at runtime ?
 Originally Posted by Jlarini
... If I use "WithEvents" I can't:
- create an array...
Emm...
 Originally Posted by me
You won't be able to. You can acchive that only with single control (not a control array) with the WithEvents...
-
Dec 22nd, 2006, 06:15 PM
#5
Hyperactive Member
Re: How to get a "click" in an component created at runtime ?
Jlarini,
I decided to cheat when I needed an array of command buttons by creating a command button at design time that was visible=false and using that command button to make copies of.
Two major benefits for me to using this method were:
firstly I had multiple copies of the command buttons click event and second It was of great benefit to me to have the first usable button in my array numbered as 1.
Steve
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
|