1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
Australia
Posts
24
Rep Power
0

#### Button not working

I'm trying to make an application that will be able to convert temperatures, for the sake of consistency, I'm also making it so that celsius will be displayed as celcius so that the program works completly fine
Code:
```If btnCelsius.Enabled = True Then
If txtCelsius.Text = "" Then
txtCelsius2.Text = txtCelsius.Text
End If
If txtCelsius.Text = "" Then
txtCelsius2.Text = Int((txtFahrenheit.Text - 32) * 5 / 9)
End If

End If```
The convert to celsius button won't convert celsius to celsius, which is odd since it wouldn't really be a conversion, can anybody help me with this problem?
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2011
Posts
289
Rep Power
45
Your second If statement does absolutely nothing. It says that if txtCelsius is blank, then make it blank. The code below automatically changes the other text box if there is a change.
Code:
```Dim flgChng As Boolean

Private Sub txtCelsius_Change()
flgChng = True
End Sub

Private Sub txtCelsius_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then SendKeys "{TAB}", 1
End Sub

Private Sub txtCelsius_LostFocus()
If flgChng Then
txtFahrenheit.Text = Int(Val(txtCelsius.Text) * 9 / 5) + 32
flgChng = False
End If
End Sub

Private Sub txtFahrenheit_Change()
flgChng = True
End Sub

Private Sub txtFahrenheit_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then SendKeys "{TAB}", 1
End Sub

Private Sub txtFahrenheit_LostFocus()
If flgChng Then
txtCelsius.Text = Int((Val(txtFahrenheit.Text) - 32) * 50 / 9) / 10
flgChng = False
End If
End Sub```
To prevent the calculation on every keystroke, I have used a flag to detect that a change was made, and when the focus is shifted to another control, the calculation is then made. I added code to TAB when the "ENTER" key is used. Text boxes are Variant, so calculations will be made to double precision. To prevent many decimal places being displayed, I used the "Int" function, with one decimal place for the Celsius calculation to give it the same precision as Fahrenheit.

J.A. Coutts
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
Australia
Posts
24
Rep Power
0
I've copied the code for txtCelsius and it saya that SendKeys is a type and cannot be used as an expression
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2011
Posts
289
Rep Power
45
Originally Posted by Problematic
I've copied the code for txtCelsius and it saya that SendKeys is a type and cannot be used as an expression
Sendkeys is a valid command in VB6. If you are using something else (such as .NET), you will have to check the Help file for the correct syntax.

J.A. Coutts
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Location
Australia
Posts
24
Rep Power
0
Oh, I'm using VB 2010, I implemented your code to get a final application that works