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;
}
Property window