I want to create a simple seat reservation for my application

Aaron soggi 246 Reputation points
2021-03-20T23:05:38.507+00:00

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 "availableSeat" it will turn green. If the user attempts to select another seat with the status "availableSeat" i want the application to throw an error message. "please unmatch your current seat, before selecting another" etc. Below is the codee that 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 user clicks on a picture box that is set as "seatTaken", i simply just want it to change back to "availableSeat". Secondly if the user selects a seat which is set as availablethey still receive an error message which is not what i want.

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

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();
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,924 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,341 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,556 Reputation points
    2021-03-21T02:05:06.443+00:00

    Hello,

    First off the tag indicates VB.NET but you have C# code hence I'm doing C#.

    I would setup a custom PictureBox, in this case I placed it in a class project but it can be in the forms project.

    using System.Windows.Forms;  
      
    namespace ControlLibrary  
    {  
        public class SeatPictureBox : PictureBox  
        {  
            public string Row { get; set; }  
            public int Number { get; set; }  
            public string Seat => $"{Row}{Number}";  
            public bool Available { get; set; }  
        }  
    }  
      
    

    On the form place one of the PictureBox controls from above for each seat. Add a ImageList with images to use in the PictureBox controls.

    • Create a private variable to hold the PictureBoxes
    • In the form constructor, setup the click event for each PictureBox.
    • Also in the form constructor setup the image for each PictureBox, if this were to come from a database table you would need to determine (easy enough) which image to use for available or unavailable.
    • Finally invoke a method to show status of each seat (optional but if nothing else is good for debugging)
    • The Click event handles changing images
    • The language extension is optional for displaying.debugging.

    Form code (each image in the image list is named appropriately)

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Windows.Forms;  
    using ControlLibrary;  
      
    namespace ReservationDemo  
    {  
        public partial class Form1 : Form  
        {  
            private readonly List<SeatPictureBox> _seatPictureBoxes;  
            public Form1()  
            {  
                InitializeComponent();  
      
                _seatPictureBoxes = Controls.OfType<SeatPictureBox>().ToList();  
                foreach (var seatPictureBox in _seatPictureBoxes)  
                {  
                    seatPictureBox.Click += SeatPictureBoxOnClick;  
                    seatPictureBox.Image = imageList1.Images["Available"];  
                }  
      
                UpdateStatus();  
            }  
      
            private void SeatPictureBoxOnClick(object sender, EventArgs e)  
            {  
                var pb = (SeatPictureBox) sender;  
                if (pb.Available)  
                {  
                    pb.Image = imageList1.Images["Unavailable"];  
                    pb.Available = false;  
                }  
                else  
                {  
                    pb.Image = imageList1.Images["Available"];  
                    pb.Available = true;  
                }  
      
                UpdateStatus();  
            }  
            private void UpdateStatus()  
            {  
                listBox1.Items.Clear();  
                  
                foreach (var box in _seatPictureBoxes.OrderBy(seatPictureBoxx => seatPictureBoxx.Seat))  
                {  
                    listBox1.Items.Add($"{box.Seat} is {box.Available.ToAvailable()}");  
                }  
            }  
        }  
        public static class BooleanExtensions  
        {  
            public static string ToAvailable(this bool value) => value ? "Available" : "Unavailable";  
        }  
    }  
      
    

    Lastly you can always use the following if you don't want a seat to be available after becoming unavailable

    var pb = (SeatPictureBox) sender;  
      
    if (pb.Available == false)  
    {  
        MessageBox.Show($"{pb.Seat} is not available, select another seat");  
        return;  
    }  
    

    79873-reservation.png

    Property window
    79789-p1.png

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Aaron soggi 246 Reputation points
    2021-03-21T14:53:36.23+00:00

    Out of all of the forums i have used online, this has by FAR been the most useful.
    Thank you so much! I appreciate the added comments too, it allowed me to understand everything a lot more :)

    I have just one more question. When the user control has loaded i would like to change the seats that have already been taken to red.

    This will depend on the schedule that has been selected. in my bookings table i have a foreign key ScheduleID, which represents the coach schedule that the user will select before reserving their seat.

    so I will need to do "SELECT seatNo FROM Bookings WHERE ScheduleId = 2002" etc

    This will get all of the seat Nos that have already been booked by other users, but then i'm unsure on how to store this data so i can then
    compare this data to the pictureBoxes in the user control.

    This is what i have so far:

       public void GetSeatNumbers(string query) //retrieveing data from database.
            {
                connection.Open();
                SqlCommand sqlCommand = new SqlCommand(query, connection);
                SqlDataReader reader;
                reader = sqlCommand.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("seatNo", typeof(string));
                connection.Close();
            }
    
    0 comments No comments

  2. Karen Payne MVP 35,556 Reputation points
    2021-03-21T16:44:48.517+00:00

    Okay, the following class project has changed along with the front-end project.

    • Important To test the code I created the simplest database and table which can be improved on
    • There is a lack of exception handling as I know the code and my environment along with limited time to provide this code.
    • There are two data operations, one to read all seats, assign information to PictureBox controls and a Update to update the currently clicked PictureBox
    • The PictureBox controls should really be created dynamically but no time for that in the near future as I'm a) racing today b) have an article to publish.
    0 comments No comments

  3. Aaron soggi 246 Reputation points
    2021-03-21T18:11:39.91+00:00

    Thank you karen much appreciated. Very grateful for how much you have helped me. I'm having a bit of an issue when dragging the Picture box into my user control. It gives me the following error "Failed to load toolbox item 'seatPictureBox'. it will be removed from the toolbox". Do you have any idea why this may be occurring?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.