r/visualbasic • • 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

5 Upvotes

6 comments sorted by

1

u/Despresl144 Oct 26 '18

Ok, so I've fixed it so the switch button does work but the labels are not center after they switch. How do I make the labels go to a specifique place.

'**********************************************************************

'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.0

intC = (intF * 5.0 / 9.0) - 32.0

Me.txtbxEntree2.Text = intF

End Sub

Private Sub btnSwitch_Click(sender As Object, e As EventArgs) Handles btnSwitch.Click

'if this is F, then we switch the c back to c and the f back to f, otherwise we do the opposite

If (Me.lblCelsius.Text = "Fahreneheit") Then

Me.lblCelsius.Text = "Celsius"

Me.lblFahrenheit.Text = "Fahreneheit"

ElseIf (Me.lblCelsius.Text = "Celsius") Then

Me.lblCelsius.Text = "Fahreneheit"

Me.lblFahrenheit.Text = "Celsius"

End If

End Sub

End Class

1

u/andrewsmd87 Web Specialist Oct 25 '18

Ok, so right now your button is always going to set the celsius to farenheight. What you need is an if statement to check what the label is and then you can switch it accordingly. Something along the lines of

Private Sub btnSwitch_Click(sender As Object, e As EventArgs) Handles btnSwitch.Click

    'if this is F, then we switch the c back to c and the f back to f, otherwise we do the opposite
    if(Me.lblCelsius.Text = "Fahreneheit")
        Me.lblCelsius.Text = "Celsius"

        Me.lblFahrenheit.Text = "Fahreneheit"

        Me.lblC.Text = "°F"

        Me.lblF.Text = "°C"

    else

        Me.lblCelsius.Text = "Fahreneheit"

        Me.lblFahrenheit.Text = "Celsius"

        Me.lblC.Text = "°F"

        Me.lblF.Text = "°C"

    end if

    Me.lblCelsius.Text = "Fahreneheit"

    Me.lblFahrenheit.Text = "Celsius"

   

End Sub

The concept here is you that you know they will all always either be f or c, and based on what one is, you can set the rest.

Now, to make it do the calculations correctly, apply the same logic to get the set the variables. In your button1 click event, check one of the labels .text. If it's celsius set your intc to that textbox, otherwise set your int f to it.

Also, you'll want to change intF and C to doubles or decimals as I mentioned in the other post. Using ints means if someone were to type 25.4, it would lose the .4.

I think the point of this part is to help you with the concept that variables can be assigned values dynamically.

Naming your labels lblCelsius is probably an in-accurate name because they can be either. Not that you necessarily need to change them at this point, just something to think about.

You're coming along nicely with this assignment BTW

1

u/Despresl144 Oct 26 '18

You're coming along nicely with this assignment BTW

Thank you very very much sir!

1

u/Despresl144 Oct 26 '18

'if this is F, then we switch the c back to c and the f back to f, otherwise we do the opposite
if(Me.lblCelsius.Text = "Fahreneheit")
Me.lblCelsius.Text = "Celsius"
Me.lblFahrenheit.Text = "Fahreneheit"
Me.lblC.Text = "°F"
Me.lblF.Text = "°C"
else
Me.lblCelsius.Text = "Fahreneheit"
Me.lblFahrenheit.Text = "Celsius"
Me.lblC.Text = "°F"
Me.lblF.Text = "°C"
end if
Me.lblCelsius.Text = "Fahreneheit"
Me.lblFahrenheit.Text = "Celsius"

End Sub

The "if else2 thing does not work. It switches the labels ones but does not switch it again

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.

1

u/andrewVladmirov Oct 26 '18

Just use a boolean variable.. name is something like: "isCelcius".. if true then celcius to farenheit, if false, then vise vera..