0

Hi there, i have developed this code (with a bit of help) that will generate a grid of pictureboxes (function as buttons) which reserve individual seats. This is my code:

Public Class Form1
    Dim i As Integer
    Dim j As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Me.i = 0 To My.Settings.Rows
            For Me.j = 0 To My.Settings.Collums
                Dim Seatpicture As New PictureBox
                Seatpicture.Location = New Point(10 + (j * 45), 15 + (i * 80))
                Seatpicture.ImageLocation = (My.Settings.DriveLetter & ":\Clear.gif")
                Seatpicture.SizeMode = PictureBoxSizeMode.StretchImage
                Seatpicture.Size = New Size(40, 78)
                Seatpicture.Load()
                Me.Controls.Add(Seatpicture)
                AddHandler Seatpicture.MouseEnter, AddressOf Mouse_Enter
                AddHandler Seatpicture.MouseLeave, AddressOf Mouse_Leave
                AddHandler Seatpicture.MouseClick, AddressOf Mouse_Click
            Next
        Next
    End Sub
    Private Sub Mouse_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Clear.gif") Then
            DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Blue.gif")
        End If
    End Sub
    Private Sub Mouse_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Blue.gif") Then
            DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Clear.gif")
        End If
    End Sub
    Private Sub Mouse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Blue.gif") Then
            DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Green.gif")
            ActualTotal.Text = ActualTotal.Text + 5

        Else
            DirectCast(sender, PictureBox).ImageLocation = (My.Settings.DriveLetter & ":\Blue.gif")
            ActualTotal.Text = ActualTotal.Text - 5

        End If
    End Sub

Image of form 1:
http://postimg.org/image/winqc1w3j/
(Green is selected, blue is highlighted)

My problem is that i want to make the program remember which seats were selected, but i have no way to identify each individual picturebox (with the exception of the sender function, which can't be used here) I want to put my pictureboxes in a 2D array which remembers the Row and collumn of the picturebox, but i am unsure how to do this.
Can anyone help?
Thanks.

Edited by harley.roberts.56

2
Contributors
1
Reply
13
Views
3 Years
Discussion Span
Last Post by Reverend Jim
0

Here is some sample code. I just coded this up using simple BackGround colour changes to indicate state but with a little work you could change it to use images. It uses a 2-D array to reference the PictureBoxes and you can iterate over the array to determine the number of seats free/sold by comparing the current BG Colour to to programs Consts. I use the PictureBox Tag property to save the corresponding row and column values.

Public Class Form1

    'Defines the size and properties of the grid

    Private Const NumRows As Integer = 5
    Private Const NumCols As Integer = 8
    Private Const PbxSize As Integer = 50
    Private Const PbxPadd As Integer = 5
    Private Const xOffset As Integer = 10
    Private Const yOffset As Integer = 10

    'Replace this with code to use images rather than colors

    Private SeatFree As Color = Color.Green
    Private SeatSold As Color = Color.Red
    Private SeatCurr As Color = Color.Yellow

    'This saves the state during MouseEnter

    Private PrevState As Color

    'This isn't really necessary unless you want to be able to reference specific
    'controls by row and column.

    Private MyGrid(NumRows, NumCols) As PictureBox

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Create the control grid

        Dim xpos As Integer
        Dim ypos As Integer

        For row As Integer = 0 To NumRows - 1

            ypos = yOffset + row * (PbxSize + PbxPadd)

            For col As Integer = 0 To NumCols - 1

                xpos = xOffset + col * (PbxSize + PbxPadd)

                Dim p As New PictureBox

                p.Location = New Point(xpos, ypos)
                p.Size = New Size(PbxSize, PbxSize)
                p.BackColor = SeatFree
                p.Tag = New Point(row, col)

                AddHandler p.Click, AddressOf PictureBox_Click
                AddHandler p.MouseEnter, AddressOf PictureBox_MouseEnter
                AddHandler p.MouseLeave, AddressOf PictureBox_MouseLeave

                MyGrid(row, col) = p
                Me.Controls.Add(p)

            Next

        Next

    End Sub

    'Toggle the state of the currently active control

    Private Sub PictureBox_Click(sender As System.Object, e As System.EventArgs)
        Dim p As PictureBox = sender
        p.BackColor = IIf(PrevState = SeatFree, SeatSold, SeatFree)
        PrevState = p.BackColor
        Me.Text = "Toggled control at " & p.Tag.ToString
    End Sub

    'Flag the control under the mouse as current

    Private Sub PictureBox_MouseEnter(sender As System.Object, e As System.EventArgs)
        Dim p As PictureBox = sender
        PrevState = p.BackColor
        p.BackColor = SeatCurr
        Me.Text = "Current control is " & p.Tag.ToString
    End Sub

    'Return the previously current control to its original state

    Private Sub PictureBox_MouseLeave(sender As System.Object, e As System.EventArgs)
        Dim p As PictureBox = sender
        p.BackColor = PrevState
    End Sub

End Class

Edited by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.