[go: up one dir, main page]

0% found this document useful (0 votes)
18 views4 pages

Solution CS411 Assignment 2

The document contains XAML and C# code for a simple WPF application that converts Fahrenheit to Celsius. It includes a user interface with labels, a text box for input, and a button to trigger the conversion. The C# code handles the button click event, performs the conversion, and displays the result or an error message if the input is invalid.

Uploaded by

wfazal912
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)
18 views4 pages

Solution CS411 Assignment 2

The document contains XAML and C# code for a simple WPF application that converts Fahrenheit to Celsius. It includes a user interface with labels, a text box for input, and a button to trigger the conversion. The C# code handles the button click event, performs the conversion, and displays the result or an error message if the input is invalid.

Uploaded by

wfazal912
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/ 4

XAML Code

<Grid Margin="10">

<StackPanel VerticalAlignment="Center">

<Label Content="BC123456789"

FontSize="26"

FontWeight="Bold"

HorizontalAlignment="Center"

Margin="0,0,0,20"/>

<Label Content="Input Fahrenheit:"

FontSize="16"

FontWeight="Bold"

Margin="0,0,0,5"/>

<TextBox Name="FahrenheitInput"

FontSize="16"

Height="30"

Margin="0,0,0,15"/>

<Button Content="To Celsius"

Name="ConvertButton"

Background="Green"

Foreground="White"

FontSize="16"

FontWeight="Bold"
Height="40"

Click="ConvertButton_Click"

Margin="0,0,0,15"/>

<Label Name="ResultLabel"

FontSize="16"

FontWeight="Bold"

Content="Result:"

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

</StackPanel>

</Grid>

</Window>
C# Code

using System;

using System.Windows;

using System.Windows.Controls;

namespace BC123456789

public partial class MainWindow : Window

public MainWindow()

InitializeComponent();

private void ConvertButton_Click(object sender, RoutedEventArgs e)

try

double fahrenheit = double.Parse(FahrenheitInput.Text);

double celsius = (fahrenheit - 32) * 5 / 9;

ResultLabel.Content = $"Result: {celsius:F2} °C";

catch (FormatException)

{
MessageBox.Show("Please enter a valid number.", "Input Error",
MessageBoxButton.OK, MessageBoxImage.Error);

You might also like