Creating A Complete C# Application

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Creating a complete C# application with a graphical user interface (GUI) as per

your requirements is quite involved, but I can provide you with a step-by-step
outline and code snippets to help you get started. You can use Windows Forms for
creating the GUI.

Here's a simplified version of the code. You will need to create a Windows Forms
Application and add the necessary controls (TextBox, Label, PictureBox, GroupBox,
RadioButtons, Buttons, ComboBox, and ListBox) to your form using the Visual Studio
or your preferred C# IDE.

Create a Windows Forms Application project in Visual Studio.


Add the required controls to the form. Set their properties and names accordingly.
Here's a simplified version of the code:

csharp
Copy code
using System;
using System.Windows.Forms;

namespace VehicleRentalApp
{
public partial class MainForm : Form
{
// Define variables to hold user input and calculation results
private double baseCost = 50.0;
private double additionalCost = 0.0;

public MainForm()
{
InitializeComponent();
// Initialize the PictureBox to the default image
pictureBoxVehicle.Image = Properties.Resources.car; // Assuming you
have images named car.png, boat.png, and bike.png in your Resources folder
}

private void comboBoxVehicle_SelectedIndexChanged(object sender, EventArgs


e)
{
// Update the PictureBox based on the selected vehicle
if (comboBoxVehicle.SelectedIndex == 0)
pictureBoxVehicle.Image = Properties.Resources.car;
else if (comboBoxVehicle.SelectedIndex == 1)
pictureBoxVehicle.Image = Properties.Resources.boat;
else if (comboBoxVehicle.SelectedIndex == 2)
pictureBoxVehicle.Image = Properties.Resources.bike;
}

private void numericUpDownDays_ValueChanged(object sender, EventArgs e)


{
// Calculate additional cost based on the selected vehicle
if (comboBoxVehicle.SelectedIndex == 0)
additionalCost = 20.0;
else if (comboBoxVehicle.SelectedIndex == 1)
additionalCost = 15.0;
else if (comboBoxVehicle.SelectedIndex == 2)
additionalCost = 10.0;

// Calculate and display the total cost


double totalCost = baseCost * (double)numericUpDownDays.Value +
additionalCost;
labelTotalCost.Text = "Total Cost: $" + totalCost.ToString("0.00");
}

private void buttonConfirm_Click(object sender, EventArgs e)


{
// Create a transaction summary and add it to the ListBox
string transactionSummary = $"{textBoxFirstName.Text}
{textBoxLastName.Text}, Vehicle: {comboBoxVehicle.SelectedItem}, Days:
{numericUpDownDays.Value}, Total Cost: {labelTotalCost.Text}";
listBoxTransactions.Items.Add(transactionSummary);

// Clear input fields


textBoxFirstName.Clear();
textBoxLastName.Clear();
comboBoxVehicle.SelectedIndex = -1;
numericUpDownDays.Value = 1;
labelTotalCost.Text = "Total Cost: $0.00";
}

private void buttonClear_Click(object sender, EventArgs e)


{
// Clear all input fields
textBoxFirstName.Clear();
textBoxLastName.Clear();
comboBoxVehicle.SelectedIndex = -1;
numericUpDownDays.Value = 1;
labelTotalCost.Text = "Total Cost: $0.00";
}
}
}
In this code:

The comboBoxVehicle_SelectedIndexChanged event handler updates the PictureBox image


based on the selected vehicle.
The numericUpDownDays_ValueChanged event handler calculates and displays the total
cost based on the selected vehicle and the number of days.
The buttonConfirm_Click event handler adds a transaction summary to the ListBox and
clears the input fields.
The buttonClear_Click event handler clears all input fields.
Remember to set up your form and controls in the designer, add images for the
vehicles, and wire up the event handlers appropriately.

This code provides a basic structure for your vehicle rental application. You can
further enhance it by adding error handling, validation, and error messages as
needed.

You might also like