How to use Button array?
i want to use separate code for each button, Please Help

Dani AI

Generated

Nice explanation from . If you want truly separate code bodies per button while still using a VB6 control array, route the shared Click to per-button Subs via a naming convention. Set each button’s Tag to the target Sub name, then jump using CallByName:

' Design-time: drop Command1(0) on the form and set Index = 0.
Private Sub Form_Load()
    Dim i As Integer
    Command1(0).Tag = "DoBtn0"
    For i = 1 To 3
        Load Command1(i)                  'create at runtime
        Command1(i).Caption = "Button " & i
        Command1(i).Tag = "DoBtn" & i     'maps to a Sub
        Command1(i).Top = Command1(0).Top + i * (Command1(0).Height + 60)
        Command1(i).Left = Command1(0).Left
        Command1(i).Visible = True        'Load does not show the control
    Next
End Sub

Private Sub Command1_Click(Index As Integer)
    CallByName Me, Command1(Index).Tag, vbMethod
End Sub

Private Sub DoBtn0():  'code for button 0...  End Sub
Private Sub DoBtn1():  'code for button 1...  End Sub
Private Sub DoBtn2():  'code for button 2...  End Sub
Private Sub DoBtn3():  'code for button 3...  End Sub

Notes and gotchas:

  • You must have one element on the form at design time (Index = 0) before you can Load more. (faculty.elgin.edu)
  • Load puts a control in memory but not on screen; set Visible = True. (learn.microsoft.com)
  • Only elements created at run time can be Unloaded (not index 0). Also avoid unloading inside events like Paint/Resize to prevent Error 365. (learn.microsoft.com)

If you are actually on VB.NET (no native control arrays), wire multiple Buttons to one handler with Handles, then branch off sender as needed (similar in spirit to ’s approach): Private Sub Buttons_Click(...) Handles Button1.Click, Button2.Click, .... (learn.microsoft.com)

The Tag + CallByName pattern keeps each button’s logic isolated while still benefiting from a single Click entry point. (learn.microsoft.com)

Recommended Answers

All 7 Replies

In VB6.0, to easily create an array of command buttons, or any control for that matter, place a command button on the form, name it, and set whatever other properties you want. Then select the command button on the form in the design environment and just like you would copy and paste some text, press CTRL+C and CTRL+V. Answer yes to the msgbox and continue for as many buttons as you want.

Now, for accessing the code window, double click on one of the buttons and the code pane will come up and you should see something like...

Private Sub Command1_Click(Index As Integer)

End Sub

From there you could use either a select case statement to tell the difference between which button was pushed, like so...

Select Case Index
Case 0 'first button (could be move first if coding for recordset navigation)
Case 1 'second button (move previous (recordset naviation))
Case 2 '(Move next in reference to recordset navigation)
Case 3 '(Move last...)
End Select

Or you could use the if elseif structure...

If Index = 0 Then
ElseIf Index =1 Then
'...

Good Luck

Ok, I understand now
Thanks for help


Solved

contact me

It's been 15 years since they were last seen on DaniWeb. I doubt they contact you.

commented: It's the day of the dead in many parts of the world. +17

Yes, The Walking Dead has already aired... :)

And now, why would I contact you since I am long retired...

commented: It's alive! lol +9
commented: The DEAD Dani AI slurped up your content. As a test, see if you can opt out of DEAD, NOT ALIVE AI? +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.