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

BC220405797 CS411-Visual Programming Assignment No.2 Fall 24 Output Screenshot

This document contains the XAML and C# code for a BMI calculator application developed for a Visual Programming assignment. The XAML code defines the user interface, including labels, text boxes, and a button for calculating BMI. The C# code handles the button click event to compute BMI based on user input, with error handling for invalid entries.

Uploaded by

riazamna611
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 views6 pages

BC220405797 CS411-Visual Programming Assignment No.2 Fall 24 Output Screenshot

This document contains the XAML and C# code for a BMI calculator application developed for a Visual Programming assignment. The XAML code defines the user interface, including labels, text boxes, and a button for calculating BMI. The C# code handles the button click event to compute BMI based on user input, with error handling for invalid entries.

Uploaded by

riazamna611
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/ 6

BC220405797

CS411-Visual Programming

Assignment No.2 Fall 24

Output Screenshot:

XAML Code:

<Window x:Class="CS411_2_FALL24.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:CS411_2_FALL24"

mc:Ignorable="d"

Title="BMI calculator" Height="275" Width="400" Background="#EAD5E8">

<Grid Margin="0,0,0,-10">

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


Margin="0,0,0,10">

<Label Content="VU ID: BC220405797" Foreground="White"

HorizontalAlignment="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"


Height="30" Margin="80,200,0,0" 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;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

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 CS411_2_FALL24

/// <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 occurred: {ex.Message}", "Error",


MessageBoxButton.OK, MessageBoxImage.Error);

You might also like