Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Lab 1. Create a Trip Calculator Windows application that can be used to deter

ID: 3548478 • Letter: C

Question

C# Lab

1. Create a Trip Calculator Windows application that can be used to determine miles per gallon for a given trip. Set the Form object properties of Name, ForeColor, BackColor, Text, and AcceptButton. The form should contain labels and textboxes to allow the user to input trip destination, miles traveled, and gallons of gas consumed. Two buttons should be placed on the form. Name all objects used in program statements. When the user clicks a button that performs the calculation, display in a label the miles per gallon for that trip. The second button should be used to reset or clear textbox entries.

Sample Layout "Guide"

Steps

Open VS and create a new C# Windows Form Project called TripCalculator

Pin the Tools window to the work area if it is not open

Change the text property of the form to

Explanation / Answer

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace TripCalculator

{

public partial class Form1 : Form

{

string prompt = "Enter your destination here...";


public Form1()

{

InitializeComponent();

}


private void Form1_Load(object sender, EventArgs e)

{

txtDestination.Text = prompt;

}


private void btnCalculate_Click(object sender, EventArgs e)

{

lblMPH.Text = "Miles per gallon: " + Convert.ToString(Convert.ToDouble(txtMiles.Text) / Convert.ToDouble(txtGallons.Text));

}


private void btnReset_Click(object sender, EventArgs e)

{

txtMiles.Text = null;

txtGallons.Text = null;

lblMPH.Text = null;

txtDestination.Text = prompt;

txtDestination.Focus();

}


private void btnCalculate_Click_1(object sender, EventArgs e)

{

lblMPH.Text = "Miles per gallon: " + Convert.ToString(Convert.ToDouble(txtMiles.Text) / Convert.ToDouble(txtGallons.Text));

}


private void btnReset_Click_1(object sender, EventArgs e)

{

txtMiles.Text = null;

txtGallons.Text = null;

lblMPH.Text = null;

txtDestination.Text = prompt;

txtDestination.Focus();

}

}

}