Results 1 to 7 of 7

Thread: Convert Celsius to Fahrenheit

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Convert Celsius to Fahrenheit

    try this:

    vb Code:
    1. Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
    2.  
    3.  
    4. Dim dblFahrenheitTemp As double = 1.8
    5. Dim dblCelsiusTemp As double = 32
    6.  
    7. dim dblTemp as double
    8. double.tryparse(txtCelsius.Text,dblTemp)
    9.  
    10. lblConvertedTemp.Text = ((dblTemp  * dblFahrenheitTemp) + dblCelsiusTemp).tostring

  3. #3
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    479

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Convert Celsius to Fahrenheit

    how many decimal places do you want. here's how for zero places:

    vb Code:
    1. lblConvertedTemp.Text = Fahrenheit.tostring("f0")
    Last edited by .paul.; Apr 2nd, 2011 at 04:31 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: Convert Celsius to Fahrenheit

    Quote Originally Posted by .paul. View Post
    how many decimal places do you want. here's how for zero places:

    vb Code:
    1. lblConvertedTemp.Text = Fahrenheit).tostring("f0")

    Thank you for your help
    this is what it ended up like:

    lblConvertedTemp.Text = Fahrenheit.ToString("f1")

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width