473,854 Members | 1,784 Online

# Almost there...need help displaying result in output Label

16 New Member
I had to create a program that allows the user to input a temp and then click on either the convert to fahrenheit or convert to celsius button and then display that result within the textbox that says "user entered amount in F or C is equal to answer degrees F or C." I have the code written for the convert to Fahrenheit but do not understand how to get the converted temp to display in the text box along with the above desired text. Below is my code for the convert to fahrenheit button...

Expand|Select|Wrap|Line Numbers
1. Public Class TemperatureConverterForm
2.    ' converts celsius to fahrenheit
3.    Private Sub convertFahrenheit_Click(ByVal sender As System.Object, ByVal _
4.    e As System.EventArgs) Handles convertFahrenheit.Click
5.       ' declare variables
6.       Dim degrees As Double
7.       Dim output As String ' store output
8.       Dim fahrenheit As Double
9.
10.       ' assign values from user input
11.       degrees = Val(degreesTextBox.Text)
12.
13.       ' convert to fahrenheit
14.       fahrenheit = (9 / 5) * degrees + 32
15.
17.       output = fahrenheit
18.
19.       output = (String.Format("{0:F}", fahrenheit))
20.
21.       outputLabel.Text = output ' display result
22.
23.    End Sub
Anyone that can help me get over this last hurdle of displaying the answer with the proper text with the text box would be greatly greatly appreciated! THanks!
Apr 22 '07 #1
2 11212
SammyB
807 Recognized Expert Contributor
You use an ampersand & to put strings together (it's called concatenation).
Expand|Select|Wrap|Line Numbers
1.     Private Sub convertFahrenheit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertFahrenheit.Click
2.         ' declare variables
3.         Dim degrees As Double
4.         Dim output As String ' output string
5.         Dim input As String ' input string
6.         Dim fahrenheit As Double
7.         ' assign values from user input
8.         degrees = Val(degreesTextBox.Text)
9.         ' convert to fahrenheit
10.         fahrenheit = (9 / 5) * degrees + 32
11.         ' set strings
12.         input = (String.Format("{0:F}", degrees))
13.         output = (String.Format("{0:F}", fahrenheit))
14.         outputLabel.Text = input & "° C = " & output & "° F" ' display result
15.     End Sub
Notice that I dropped the line output = fahrenheit: it wasn't doing anything. I also created an input string, just like you did, and, finally, slung them together with ampersands.

But, it seems to me that you are only about 1/3 done. Don't you need to convert from fahrenheit to celsius, also?
Apr 22 '07 #2
lifeshortlivitup
16 New Member
You use an ampersand & to put strings together (it's called concatenation).
Expand|Select|Wrap|Line Numbers
1.     Private Sub convertFahrenheit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertFahrenheit.Click
2.         ' declare variables
3.         Dim degrees As Double
4.         Dim output As String ' output string
5.         Dim input As String ' input string
6.         Dim fahrenheit As Double
7.         ' assign values from user input
8.         degrees = Val(degreesTextBox.Text)
9.         ' convert to fahrenheit
10.         fahrenheit = (9 / 5) * degrees + 32
11.         ' set strings
12.         input = (String.Format("{0:F}", degrees))
13.         output = (String.Format("{0:F}", fahrenheit))
14.         outputLabel.Text = input & "° C = " & output & "° F" ' display result
15.     End Sub
Notice that I dropped the line output = fahrenheit: it wasn't doing anything. I also created an input string, just like you did, and, finally, slung them together with ampersands.

But, it seems to me that you are only about 1/3 done. Don't you need to convert from fahrenheit to celsius, also?
Thanks for the input. And yes, I do have to convert from fahrenheit to celsius also but I didn't include that code here because I didn't want to have my entire program on here....Thanks again for the help.
Apr 22 '07 #3