r/visualbasic • u/Despresl144 • Oct 25 '18
How to make a "Switch" Button

This is my code. Disregard the last sub, it does not work.
'**********************************************************************
'Programmeur: Lucas Després *
'Date: Octobre 2018 *
'But: De convertir les unité de mesure de la température *
'a Celcius et Fahrenheit et vice versa *
'**********************************************************************
Public Class frmConversion
Private Sub frmConversion_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AcceptButton = btnConvertir
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnConvertir.Click
'Crée un Integer pour Fahrenhei et Celcius
Dim intF As Double
Dim intC As Double
intC = Val(txtbxEntree1.Text)
intF = Val(txtbxEntree2.Text)
intF = (intC * 9.0 / 5.0) + 32
intC = (intF * 5.0 / 9.0) - 32
Me.txtbxEntree2.Text = intF
End Sub
Private Sub btnSwitch_Click(sender As Object, e As EventArgs) Handles btnSwitch.Click
Me.lblCelsius.Text = "Fahreneheit"
Me.lblFahrenheit.Text = "Celsius"
Me.lblC.Text = "°F"
Me.lblF.Text = "°C"
End Sub
End Class
1
u/sundappen Oct 25 '18
You could keep track of which way the calculations are going; from C2F or the other way around. You could use a variable "Celsius2Fahrenheit as boolean = true" for instance, to indicate from Celsius to Fahrenheit. Then you check to see if this is true/false whenever user clicks "Switch" or when the user clicks "Convert", and change labels accordingly. You use the same logic to decide which calculation is used as well.