0% found this document useful (0 votes)
19 views5 pages

Fall 2024 - CS411 - 2 - BC230203930

Uploaded by

seharbutt804
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)
19 views5 pages

Fall 2024 - CS411 - 2 - BC230203930

Uploaded by

seharbutt804
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/ 5

ASSIGNMENT # 02

CS411- VISUAL PROGRAMMING


VU ID: BC230203930

Output Screenshot:

XAML CODE:

<Window x:Class="BC230203930.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:BC230203930"
mc:Ignorable="d"
Title="BMI Calculator" Height="275" Width="400" Background="#EAD5E8">
<Grid>

<Border Background="#7FA9B3" Height="40" VerticalAlignment="Top"


Margin="0,0,0,10">
<Label Content="VU ID: BC230203930" Foreground="white"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
FontWeight="Bold" FontSize="16"/>
</Border>

<!-- Labels -->


<Label Content="Enter Your Height" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="30,50,0,0"/>
<Label Content="Enter Your Weight" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="30,100,0,0"/>
<Label Content="BMI" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,200,0,0"/>

<!-- Textboxes -->


<TextBox x:Name="txtHeight" HorizontalAlignment="Left" VerticalAlignment="Top"
Height="30" Margin="150,50,0,0" Width="150"/>
<TextBox x:Name="txtWeight" HorizontalAlignment="Left" VerticalAlignment="Top"
Height="30" Margin="150,100,0,0" Width="150"/>
<TextBox x:Name="txtBMI" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="80,200,0,0" Height="30" Width="150" Background="#DFEC8F"
IsReadOnly="True"/>

<!-- Buttons -->


<Button Content="Calculate BMI" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="100,150,0,0" Width="150" Background="#99B4D1"
Click="CalculateBMI_Click"/>
</Grid>
</Window>

C# Code:

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 BC230203930
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void CalculateBMI_Click(object sender, RoutedEventArgs e)


{

try
{
double weight = double.Parse(txtWeight.Text);
double height = double.Parse(txtHeight.Text);

if (weight <= 0 || height <= 0)


{
MessageBox.Show("Weight and Height must be positive values.", "Input Error",
MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
height = height / 100;
double bmi = weight / (height * height);
txtBMI.Text = bmi.ToString("F2");
}
catch (FormatException)
{
MessageBox.Show("Please enter valid numeric values for Weight and Height.",
"Input Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (Exception ex)
{
MessageBox.Show($"An unexpected error occured: {ex.Message}", "Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}

You might also like