Changing picture box image based on seat status

sock1992

Well-known member
Joined
May 20, 2020
Messages
99
Programming Experience
Beginner
I want to create a simple seat reservation system where the picture box will change to another image once the user has clicked the picture box. If the image is set as "available" it will turn green. If the user attempts to select another seat with the status "available" i want the application to throw an error message. "please unmatch your current seat, before selecting another" etc. Below is what i have so far, I've been trying to do this for a while now and I'm honestly lost, the annoying thing is it seems like its a simple fix. I was thinking of making a separate class and monitoring the seat status that way, but i wouldn't even know where to start.

The problem i have is the user is still given an error message even if the image is set as seatTaken, also if the user selects a seat which is set as available available they still receive an error message which is not what i want.

All picture boxes are part of the same click event aswell. Can anyone help? i'd really appreciate it

C#:
        Bitmap availableSeat = Properties.Resources.availableSeatt;
        Bitmap seatTaken = Properties.Resources.provisional;

        private void Button_Click(object sender, EventArgs e)
        {
            var pb = (PictureBox)sender;
         
            if (n == 1 || pb.Image == seatTaken && n ==1)
            {
                MessageBox.Show("Please unmatch seat");
            }

            if (pb.Image == availableSeat)
            {
               
                pb.Image = seatTaken;
                n = 1;
             
            }
            else
            {
                pb.Image = availableSeat;
                n = 0;
            }
         
            selectedSeatNo = pb.Tag.ToString();
        }
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,250
Location
Sydney, Australia
Programming Experience
10+
Just store the currently taken seat in a field. You then act based on whether that field is null or not. E.g.
C#:
private PictureBox currentSeat;

private void SeatPictureBoxes_Click(object sender, EventArgs e)
{
    var selectedSeat = (PictureBox) sender;

    if (currentSeat == null)
    {
        // No current seat and new seat selected.

        // Update selectedSeat here.

        currentSeat = selectedSeat;
    }
    else if (currentSeat == selectedSeat)
    {
        // Current seat unselected.

        // Update selectedSeat here.

        currentSeat = null;
    }
    else
    {
        // New seat selected while other current seat.

        // Display error prompt.
    }
}
 
Top Bottom