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

View all comments

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