Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

VISUAL PROGRAMMING LAB

LAB 7
Making a Unit Converter using Event Handlers

Name : Burhan Ahmed


Enrollment No. : 01-134172-065
Class : BSCS 5-A
Subject : Visual Programming Lab
Lab Assistant : Sir Ali Mirza

Objectives
•Learn to use events on form elements
•Making an application that responds in realtime using events
•Using windows forms combo box

Tools Used
• Studio 2019 Community Version
BSCS 5-A Burhan Ahmed Satti 01-134172-065

Task No. 7.1


Make a Unit Converter using windows forms.
Solution
Contents of Program.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VP_Lab_0601
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ConverterForm());
}
}
}

}
Contents of Form1.cs File
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VP_Lab_0601
{
delegate string ConvertFunction(float ConvertFrom);
public partial class ConverterForm : Form
{
ConvertFunction ConvertFirst;
ConvertFunction ConvertSecond;
public ConverterForm()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


{
string unit = cb_Units.SelectedItem.ToString();
if (unit == "Kilometers & Miles")
{
l_ConvertFrom.Text = "Kilometers";
l_ConvertTo.Text = "Miles";
}
else if (unit == "Meters & Inches")
{
l_ConvertFrom.Text = "Meters";
l_ConvertTo.Text = "Inches";
}
else if (unit == "Kilograms & Gallons")
{
l_ConvertFrom.Text = "Kilograms";
l_ConvertTo.Text = "Gallons";
}
else if (unit == "Liters & Gallons")
{

Visual Programming Lab Page 2 of 6


BSCS 5-A Burhan Ahmed Satti 01-134172-065

l_ConvertFrom.Text = "Liters";
l_ConvertTo.Text = "Gallons";
}
else if (unit == "Tons & Kilos")
{
l_ConvertFrom.Text = "Tons";
l_ConvertTo.Text = "Kilos";
}
}

private void l_ConvertFrom_Click(object sender, EventArgs e)


{

private void b_Exit_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void ConverterForm_Load(object sender, EventArgs e)


{
MessageBox.Show("Welcome To Converter", "Welcome Message", MessageBoxButtons.OK);
}

private void ConverterForm_FormClosing(object sender, FormClosingEventArgs e)


{
Application.Exit();
}

private void ConverterForm_FormClosed(object sender, FormClosedEventArgs e)


{
Application.Exit();
}

private void tb_ConvertFrom_TextChanged(object sender, EventArgs e)


{
string unit = cb_Units.SelectedItem.ToString();
if (unit == "Kilometers & Miles")
{
try
{
tb_ConvertTo.Text = Converter.ConvertKMtoMiles(float.Parse(tb_ConvertFrom.Text));
}
catch
{
tb_ConvertTo.Text = "";
}
}
else if (unit == "Meters & Inches")
{
try
{
tb_ConvertTo.Text = Converter.ConvertMeterstoInches(float.Parse(tb_ConvertFrom.Text));
}
catch
{
tb_ConvertTo.Text = "";
}
}
else if (unit == "Kilograms & Gallons")
{
try
{
tb_ConvertTo.Text = Converter.ConvertKilostoGallon(float.Parse(tb_ConvertFrom.Text));
}
catch
{
tb_ConvertTo.Text = "";
}
}
else if (unit == "Liters & Gallons")
{
try
{
tb_ConvertTo.Text = Converter.ConvertLitrestoGallons(float.Parse(tb_ConvertFrom.Text));

Visual Programming Lab Page 3 of 6


BSCS 5-A Burhan Ahmed Satti 01-134172-065

}
catch
{
tb_ConvertTo.Text = "";
}
}
else if (unit == "Tons & Kilos")
{
try
{
tb_ConvertTo.Text = Converter.ConvertTonstoKilos(float.Parse(tb_ConvertFrom.Text));
}
catch
{
tb_ConvertTo.Text = "";
}
}
}

private void tb_ConvertTo_TextChanged(object sender, EventArgs e)


{
string unit = cb_Units.SelectedItem.ToString();
if (unit == "Kilometers & Miles")
{
try
{
tb_ConvertFrom.Text = Converter.ConvertMilesToKM(float.Parse(tb_ConvertTo.Text));
}
catch
{
tb_ConvertFrom.Text = "";
}
}
else if (unit == "Meters & Inches")
{
try
{
tb_ConvertFrom.Text = Converter.ConvertInchestoMeter(float.Parse(tb_ConvertTo.Text));
}
catch
{
tb_ConvertFrom.Text = "";
}
}
else if (unit == "Kilograms & Gallons")
{
try
{
tb_ConvertFrom.Text = Converter.ConvertGallonstoKilos(float.Parse(tb_ConvertTo.Text));
}
catch
{
tb_ConvertFrom.Text = "";
}
}
else if (unit == "Liters & Gallons")
{
try
{
tb_ConvertFrom.Text = Converter.ConvertLitrestoGallons(float.Parse(tb_ConvertTo.Text));
}
catch
{
tb_ConvertFrom.Text = "";
}
}
else if (unit == "Tons & Kilos")
{
try
{
tb_ConvertTo.Text = Converter.ConvertKilostoTons(float.Parse(tb_ConvertFrom.Text));
}
catch
{
tb_ConvertFrom.Text = "";
}

Visual Programming Lab Page 4 of 6


BSCS 5-A Burhan Ahmed Satti 01-134172-065

}
}
}
}
}
Contents of Converter.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VP_Lab_0601
{
class Converter
{
public static string ConvertKMtoMiles(float km)
{
return (km * 0.621371f ).ToString();
}
public static string ConvertMilesToKM(float miles)
{
return (miles * 1.60934f ).ToString();
}

public static string ConvertMeterstoInches(float meters)


{
return (meters * 39.3701f ).ToString();
}
public static string ConvertInchestoMeter(float inches)
{
return (inches * 0.0254f ).ToString();
}

public static string ConvertKilostoGallon(float kilos)


{
return (kilos * 2.20462f ).ToString();
}
public static string ConvertGallonstoKilos(float gallons)
{
return (gallons * 3.753460372602f ).ToString();
}

public static string ConvertLitrestoGallons(float litre)


{
return (litre * 0.264172f ).ToString();
}
public static string ConvertGallonstoLiters(float gallons)
{
return (gallons * 3.78541f ).ToString();
}

public static string ConvertTonstoKilos(float Tons)


{
return (Tons * 907.18f ).ToString();
}
public static string ConvertKilostoTons(float kilos)
{
return (kilos * 0.00110231f ).ToString();
}
}
}

Visual Programming Lab Page 5 of 6


BSCS 5-A Burhan Ahmed Satti 01-134172-065

Result

Conclusion
Task is done.

Visual Programming Lab Page 6 of 6

You might also like