Last active
April 2, 2023 01:01
-
-
Save warrenkc/6b108327e380b39c82c2d4e233b6bdb2 to your computer and use it in GitHub Desktop.
Convert Celcius to Fahrenheit: Powershell Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load WPF assemblies | |
Add-Type -AssemblyName PresentationFramework | |
# Define XAML code for the GUI | |
[xml]$xaml = @" | |
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="Celsius to Fahrenheit Converter" Height="250" Width="400"> | |
<Grid> | |
<Label Content="Celsius:" HorizontalAlignment="Center" Margin="0,40,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="SemiBold"/> | |
<TextBox x:Name="CelsiusTextBox" HorizontalAlignment="Center" Height="30" Margin="0,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150" FontSize="16"> | |
<TextBox.Style> | |
<Style TargetType="TextBox"> | |
<Style.Triggers> | |
<Trigger Property="IsMouseOver" Value="True"> | |
<Setter Property="Background" Value="LightGray"/> | |
</Trigger> | |
</Style.Triggers> | |
</Style> | |
</TextBox.Style> | |
</TextBox> | |
<Button x:Name="ConvertButton" Content="Convert" HorizontalAlignment="Center" Margin="0,120,0,0" VerticalAlignment="Top" Width="120" Height="30" FontSize="16"> | |
<Button.Style> | |
<Style TargetType="Button"> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="Button"> | |
<Border x:Name="border" CornerRadius="12" Background="{TemplateBinding Background}"> | |
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> | |
</Border> | |
<ControlTemplate.Triggers> | |
<Trigger Property="IsMouseOver" Value="True"> | |
<Setter TargetName="border" Property="Background" Value="DarkGray"/> | |
</Trigger> | |
</ControlTemplate.Triggers> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> | |
</Button.Style> | |
</Button> | |
<Label x:Name="ResultLabel" Content="" HorizontalAlignment="Center" Margin="0,170,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="SemiBold"/> | |
</Grid> | |
</Window> | |
"@ | |
# Create GUI from XAML | |
$reader = New-Object System.Xml.XmlNodeReader($xaml) | |
$window = [Windows.Markup.XamlReader]::Load($reader) | |
# Get named elements from the XAML | |
$CelsiusTextBox = $window.FindName("CelsiusTextBox") | |
$ConvertButton = $window.FindName("ConvertButton") | |
$ResultLabel = $window.FindName("ResultLabel") | |
# Define conversion function | |
function Convert-CelsiusToFahrenheit($celsius) { | |
return ($celsius * 9/5) + 32 | |
} | |
# Define the conversion action | |
$action = { | |
try { | |
$celsius = [double]::Parse($CelsiusTextBox.Text) | |
$fahrenheit = Convert-CelsiusToFahrenheit($celsius) | |
$ResultLabel.Content = "{0} Celsius = {1} Fahrenheit" -f $celsius, $fahrenheit | |
} catch { | |
$ResultLabel.Content = "Invalid input. Please enter a valid number." | |
} | |
} | |
# Define button click event handler | |
$ConvertButton.Add_Click($action) | |
# Add KeyDown event handler to the CelsiusTextBox | |
$CelsiusTextBox.Add_KeyDown({ | |
if ($_.Key -eq [System.Windows.Input.Key]::Enter) { | |
& $action | |
} | |
}) | |
$window.Add_Loaded({ | |
$CelsiusTextBox.Focus() | |
}) | |
$null = $window.ShowDialog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment