4

I want to generate a grid on a form in vb6 of buttons. I'm working on a reservation so I would assume generating buttons for each selection (e.g. A1, E7, etc.) would be the way to go.

However, I have not been able to figure out how to accomplish this.

I'm working on a seat reservation system. So I would want the grid to be generated from the number of seats inputted via a database (shouldn't really matter, but what the hay).

So for example if the total number of seats was 100, I would want the form to generate a 10x10 grid of buttons. When one of the buttons is clicked (each one being unique), I would be able to reserve them by modifying/adding the seat to the reserved table in a database.

Been trying to find solutions all over the place but it seems there isn't a fix answer to this in VB6.

3
  • 1
    You'll want to use a control array. Take a look at this: forums.devx.com/… Commented Feb 9, 2014 at 23:54
  • 1
    possible duplicate of Make control array in code Commented Feb 10, 2014 at 9:21
  • You could get yourself in trouble with this approach. Adding that many controls to a form will likely seriously degrade the performance of your application. You can learn a little more here: support.microsoft.com/kb/229756 Commented Feb 10, 2014 at 16:50

1 Answer 1

2

See below for what you ask for:

'1 form with
'  1 commandbutton: name=Command1  index=0
Option Explicit

Private Sub Command1_Click(Index As Integer)
  Caption = CStr(Index)
End Sub

Private Sub Form_Load()
  Dim lngIndex As Long
  For lngIndex = 1 To 100
    Load Command1(lngIndex)
  Next lngIndex
  For lngIndex = 0 To Command1.UBound
    With Command1(lngIndex)
      .Caption = CStr(lngIndex)
      .Visible = True
    End With 'With Command1(lngIndex)
  Next lngIndex
End Sub

Private Sub Form_Resize()
  Dim lngIndex As Long
  Dim sngWidth As Single, sngHeight As Single
  Dim lngRow As Long, lngCol As Long
  sngWidth = ScaleWidth / 10
  sngHeight = ScaleHeight / 10
  For lngIndex = 0 To Command1.UBound
    lngRow = lngIndex \ 10
    lngCol = lngIndex Mod 10
    Command1(lngIndex).Move lngCol * sngWidth, lngRow * sngHeight, sngWidth, sngHeight
  Next lngIndex
End Sub

Be careful though as lots of control on 1 form can slow down the perfomance a lot

If the layout of the seats is a nice grid, you might be better off using a (msflex) grid control

Another option would be to load a picture of the seat-map and let the user click on the picture after which you can use the X and Y coordinates to determine which seat was clicked ... that way you can also use different colors in the picture, and get the color on which the user clicked to make a preselection of the type of seat

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.