|
-
Apr 2nd, 2011, 02:26 PM
#1
Thread Starter
New Member
Convert Celsius to Fahrenheit
OK, I've figured out the Variable declaration and coding to make it work:
Dim Celsius, Fahrenheit As Double
Try
Celsius = CDbl(txtCelsius.Text)
Fahrenheit = 32 + (9 / 5) * CDbl(txtCelsius.Text)
lblConvertedTemp.Text = CStr(Fahrenheit)
Catch ex As Exception
MessageBox.Show("Data input must be numeric", "Error")
With txtCelsius
.Focus()
.SelectAll()
End With
End Try
End Sub
Now I'm trying to specify the number of decimals in the output lablel and not sure where to put it into the coding?
Please Help
hello,
i have what seems like a simple project that converts Celsius to Fahrenheit.
I'm having problems creating variables and get an error:
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Dim sngFahrenheitTemp As Single = 1.8
Dim sngCelsiusTemp As Single = 32
lblConvertedTemp.Text = (txtCelsius.Text "ERROR HERE" * sngFahrenheitTemp) + sngCelsiusTemp
lblConvertedTemp.Text = lblConvertedTemp.ToString
I'm gettting an "error": Option strict on dissallows implicit conversions from string to double????
I have a textbox for celsius input that I want stored and converted into a label with the Convert Button Click Event.
Last edited by Gbart; Apr 2nd, 2011 at 04:21 PM.
Reason: update on progress
-
Apr 2nd, 2011, 02:40 PM
#2
Re: Convert Celsius to Fahrenheit
try this:
vb Code:
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Dim dblFahrenheitTemp As double = 1.8
Dim dblCelsiusTemp As double = 32
dim dblTemp as double
double.tryparse(txtCelsius.Text,dblTemp)
lblConvertedTemp.Text = ((dblTemp * dblFahrenheitTemp) + dblCelsiusTemp).tostring
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 2nd, 2011, 02:56 PM
#3
Hyperactive Member
Re: Convert Celsius to Fahrenheit
You need to convert the text in the text box to a number before you can do math with it. I haven't tested this but try
Code:
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Dim sngFahrenheitTemp As Single = 1.8
Dim sngCelsiusTemp As Single = 32
Dim sngConverted as single = 0
sngConverted = (Single.Parse(txtCelsius.Text) * sngFahrenheitTemp) + sngCelsiusTemp
lblConvertedTemp.Text = sngConverted.ToString
End Sub
-
Apr 2nd, 2011, 04:23 PM
#4
Thread Starter
New Member
Re: Convert Celsius to Fahrenheit
OK, I've figured out the Variable declaration and coding to make it work:
Dim Celsius, Fahrenheit As Double
Try
Celsius = CDbl(txtCelsius.Text)
Fahrenheit = 32 + (9 / 5) * CDbl(txtCelsius.Text)
lblConvertedTemp.Text = CStr(Fahrenheit)
Catch ex As Exception
MessageBox.Show("Data input must be numeric", "Error")
With txtCelsius
.Focus()
.SelectAll()
End With
End Try
End Sub
Now I'm trying to specify the number of decimals in the output lablel and not sure where to put it into the coding?
Please Help
hello,
i have what seems like a simple project that converts Celsius to Fahrenheit.
I'm having problems creating variables and get an error:
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Dim sngFahrenheitTemp As Single = 1.8
Dim sngCelsiusTemp As Single = 32
lblConvertedTemp.Text = (txtCelsius.Text "ERROR HERE" * sngFahrenheitTemp) + sngCelsiusTemp
lblConvertedTemp.Text = lblConvertedTemp.ToString
I'm gettting an "error": Option strict on dissallows implicit conversions from string to double????
I have a textbox for celsius input that I want stored and converted into a label with the Convert Button Click Event.
-
Apr 2nd, 2011, 04:27 PM
#5
Re: Convert Celsius to Fahrenheit
how many decimal places do you want. here's how for zero places:
vb Code:
lblConvertedTemp.Text = Fahrenheit.tostring("f0")
Last edited by .paul.; Apr 2nd, 2011 at 04:31 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 2nd, 2011, 04:32 PM
#6
Thread Starter
New Member
Re: Convert Celsius to Fahrenheit
 Originally Posted by .paul.
how many decimal places do you want. here's how for zero places:
vb Code:
lblConvertedTemp.Text = Fahrenheit).tostring("f0")
Thank you for your help
this is what it ended up like:
lblConvertedTemp.Text = Fahrenheit.ToString("f1")
-
Apr 3rd, 2011, 07:20 AM
#7
Re: Convert Celsius to Fahrenheit
You don't have to use try catch for user input validation
Code:
Dim converting As Boolean = False
Dim fmt As String = "n1"
Private Sub TextBox1_TextChanged(sender As System.Object, _
e As System.EventArgs) Handles TextBox1.TextChanged
'Celsius text
If converting Then Exit Sub
converting = True
Dim c, f As Double
If Double.TryParse(TextBox1.Text.Replace(",", ""), c) Then
f = c * (9 / 5) + 32
TextBox2.Text = f.ToString(fmt)
Label1.Text = ""
Else
Label1.Text = "Invalid input"
TextBox2.Text = "Error"
End If
converting = False
End Sub
Private Sub TextBox2_TextChanged(sender As System.Object, _
e As System.EventArgs) Handles TextBox2.TextChanged
'Fahrenheit
If converting Then Exit Sub
converting = True
Dim c, f As Double
If Double.TryParse(TextBox2.Text.Replace(",", ""), f) Then
c = (f - 32) * (5 / 9)
TextBox1.Text = c.ToString(fmt)
Label1.Text = ""
Else
Label1.Text = "Invalid input"
TextBox1.Text = "Error"
End If
converting = False
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|