|
-
Aug 2nd, 2000, 04:08 AM
#1
Thread Starter
New Member
I am trying to write a database driven application which will have a different number of command buttons (from 0 - 20) depending on the result of database query. How can I add command buttons to a form at runtime?
-
Aug 2nd, 2000, 04:48 AM
#2
Hi,
Put one CommandButton on your Form.
Give it the Index 0.
Load as many Buttons you like with Note: the loaded controls are initially invisible.
Regards
da_bob
_______________
-
Aug 2nd, 2000, 06:00 AM
#3
Well ....
Andrew, if you are using VB5, the first reply answers your query. If you have VB6, however, you can add a control at runtime:
Code:
'Declarations section
Dim WithEvents newButton As CommandButton
Private Sub Form_Activate()
Set newButton = Me.Controls.Add("VB.CommandButton", _
"Button1")
End Sub
Check out the MSDN help for adding controls dynamically. VB.CommandButton is the name of the class you are using, while Button1 will be the name of the command button.
Please reply if you have any doubts on this subject.
-
Aug 2nd, 2000, 06:18 AM
#4
me again 
I don't know anything about VB5 since I'm a programmer for 1.5 years only.
The disadvantage with your solution honeybee is that you don't have a benefit of adding the Buttons dynamically because
you need to declare the maximum number of Buttons you want to use in order to sink their events- this is not very dynamical.
Regards,
da_bob
____________
-
Aug 2nd, 2000, 07:05 AM
#5
_______
<?>
Code:
'as per msdn dynamically added command button
Option Explicit
Private WithEvents btnObj As CommandButton
Private Sub btnObj_Click()
MsgBox "This is a dynamically added button."
End Sub
Private Sub Form_Load()
Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
With btnObj
.Visible = True
.Width = 2000
.Caption = "Hello"
.Top = 1000
.Left = 1000
End With
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 2nd, 2000, 08:11 AM
#6
Here is a short and simple method.
Code:
Controls.Add "VB.CommandButton", "Button1"
Me!Button1.Move 0, 0
Me!Button1.Caption = "New Button"
Me!Button1.Visible = True
-
Aug 2nd, 2000, 08:17 AM
#7
Well ....
Exactly, HeSaidJoe!
da_bob,
Remember, you can dynamically add controls, but not events. What we actually have to do is when we declare a CommandButton object with
Code:
Dim Withevents newCommandButton as CommandButton
we also have to declare any events that should be associated with the command buttons that we are going to add dynamically. It's similar to the method you use with control arrays. So you are wrong when you say that we need to declare the max. no. of buttons to add to sink in the events. There is only one event.
-
Aug 2nd, 2000, 08:24 AM
#8
Sounds cool .
Could you give me a complete code-sample too?
I never managed to add a control array dynamically with the Controls.Add function.
Regards
da_bob
____________
-
Aug 7th, 2000, 04:32 AM
#9
Well ....
da_bob, sorry for the late reply. But you cannot add a control array by adding elements at runtime. See the thread on Setting Index Property of dynamically added controls in the General VB section for more details.
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
|