Public Class Form1

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim fahrenheit As Double
        Dim celsius As Double

        ' Validate input
        If Double.TryParse(txtFahrenheit.Text, fahrenheit) Then
            celsius = (fahrenheit - 32) * 5 / 9
            lblResult.Text = "Celsius: " & Math.Round(celsius, 2).ToString()
        Else
            MessageBox.Show("Please enter a valid number.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        lblResult.Text = ""
        txtFahrenheit.Clear()
        txtFahrenheit.Focus()
    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Dim result As DialogResult
        result = MessageBox.Show("Do you want to quit?", "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        lblResult.Text = ""
        txtFahrenheit.Clear()
        txtFahrenheit.Focus()

        If result = DialogResult.Yes Then
            Me.Close() ' Closes the current form
        End If
    End Sub
End Class

Leave a Reply

Your email address will not be published. Required fields are marked *