0% found this document useful (0 votes)
6 views3 pages

CS411 Assignment 2

This document presents the solution for Assignment 2 of CS411 - Visual Programming, created by Athar Jehangir. It includes a screenshot of the output, the XAML code for a BMI calculator interface, and the C# code that calculates BMI based on user input for height and weight. The program handles exceptions by prompting the user to enter valid numbers if the input is incorrect.

Uploaded by

atharjehangir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

CS411 Assignment 2

This document presents the solution for Assignment 2 of CS411 - Visual Programming, created by Athar Jehangir. It includes a screenshot of the output, the XAML code for a BMI calculator interface, and the C# code that calculates BMI based on user input for height and weight. The program handles exceptions by prompting the user to enter valid numbers if the input is incorrect.

Uploaded by

atharjehangir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CS411 – Visual Programming

Assignment number 2
Solution
Stu ID: BC190402805
Name: Athar Jehangir

//Put screenshot of output here


1. Pressing "Calculate BMI" button

//Put your XAML code (code of the MainWindow.xaml file) here


<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="BMI Calculator" Height="300" Width="370">
<Grid>
<Grid.Background>
<SolidColorBrush Color="MediumPurple" />
</Grid.Background>
<Rectangle Height="30"
VerticalAlignment="Top"
Fill="SkyBlue"/>
<TextBlock Text="VU ID: BC190402805"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="0,5,0,0"
Foreground="Black"
FontWeight="Bold"/>
<Label Content="Enter Your Height"
HorizontalAlignment="Left"
Margin="10,50,0,0"
VerticalAlignment="Top"/>
<TextBox x:Name="textHeight"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="23"
Width="150"
Margin="120,50,0,0"
TextWrapping="Wrap"/>
<Label Content="Enter Your Weight"
HorizontalAlignment="Left"
Margin="10,100,0,0"
VerticalAlignment="Top"/>
<TextBox x:Name="textWeight"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="23"
Width="150"
Margin="120,100,0,0"
TextWrapping="Wrap"/>
<Button x:Name="buttonCalculate"
Content="Calculate BMI"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="0,150,0,0"
Height="30"
Width="120"
Background="Silver"
Foreground="Black"
FontWeight="Bold"
Click="buttonCalculateClick"/>
<Label Content="BMI"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,220,0,0"/>
<TextBox x:Name="textBMI"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="23"
Width="150"
Margin="120,220,0,0"
TextWrapping="Wrap"
Background="LightYellow"
IsReadOnly="True"/>
</Grid>
</Window>

//Put your C# code (code of the MainWindow.xaml.cs file) here


using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void buttonCalculateClick(object sender, RoutedEventArgs e) {

try {
double height = double.Parse(textHeight.Text);
double weight = double.Parse(textWeight.Text);

height = height / 100;


double bmi = weight / (height * height);
textBMI.Text = Math.Round(bmi).ToString();
}
catch(Exception ex) {

MessageBox.Show("Please enter a valid numbers for height and weight.",


"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
}
}

You might also like