Building a Parking Lot Management System with C# and Windows Forms
A parking lot management system addresses the inefficiencies of traditional methods by automaitng vehicle entry and exit, monitoring space availabliity, and handling billing. This implementation uses C# with Windows Forms for the user interface, SQL Server for data storage, and ADO.NET for database operations, enhanced with GDI+ to visualization and multi-threading for concurrency.
Vehicle Entry and Exit Management
This module captures vehicle details, assigns available parking spots, and logs timestamps. A simulated license plate recognition process initiates the workflow.
// Method to process vehicle entry
private void ProcessVehicleArrival()
{
string plateNumber = inputPlateField.Text.Trim();
if (string.IsNullOrWhiteSpace(plateNumber))
{
ShowAlert("Plate number is required.", "Input Error");
return;
}
int freeSpotId = LocateEmptySpot();
if (freeSpotId == 0)
{
ShowAlert("No parking spaces available.", "Full Capacity");
return;
}
var arrivalRecord = new ArrivalLog
{
PlateNumber = plateNumber,
ArrivalTimestamp = DateTime.Now,
AssignedSpotId = freeSpotId
};
using (var dataContext = new ParkingDataContext())
{
dataContext.ArrivalLogs.Add(arrivalRecord);
var spot = dataContext.ParkingSpaces.Find(freeSpotId);
spot.Occupied = true;
dataContext.SaveChanges();
}
RefreshDisplay();
ShowAlert($"Vehicle {plateNumber} parked at spot {freeSpotId}.", "Confirmed");
inputPlateField.Clear();
}
// Find an unoccupied parking spot
private int LocateEmptySpot()
{
using (var dataContext = new ParkingDataContext())
{
var emptySpot = dataContext.ParkingSpaces.FirstOrDefault(ps => !ps.Occupied);
return emptySpot?.SpaceId ?? 0;
}
}
Real-Time Parking Space Monitoring
A graphical layout displays spot occupancy, updated dynamically. Users can view availability and navigate to specific locations.
// Render the parking lot layout
private void RenderParkingGrid(Graphics graphics)
{
int spaceWidth = 60;
int spaceHeight = 30;
int margin = 10;
int rowCount = 10;
int columnCount = 20;
List<ParkingSpace> spaces;
using (var dataContext = new ParkingDataContext())
{
spaces = dataContext.ParkingSpaces.ToList();
}
foreach (var space in spaces)
{
int rowIndex = (space.SpaceId - 1) / columnCount;
int columnIndex = (space.SpaceId - 1) % columnCount;
int xPos = columnIndex * (spaceWidth + margin) + margin;
int yPos = rowIndex * (spaceHeight + margin) + margin;
Brush fillColor = space.Occupied ? Brushes.Red : Brushes.Green;
graphics.FillRectangle(fillColor, xPos, yPos, spaceWidth, spaceHeight);
graphics.DrawString(space.SpaceId.ToString(), this.Font, Brushes.Black, xPos + 5, yPos + 5);
graphics.DrawRectangle(Pens.Black, xPos, yPos, spaceWidth, spaceHeight);
}
}
// Periodic update of parking status
private void UpdateTimer_Tick(object sender, EventArgs e)
{
RefreshDisplay();
}
private void RefreshDisplay()
{
layoutPanel.Invalidate();
using (var dataContext = new ParkingDataContext())
{
int freeCount = dataContext.ParkingSpaces.Count(ps => !ps.Occupied);
availableLabel.Text = $"Available spots: {freeCount}";
}
}