Reputation: 318
I'm in my first semester of college, and we're doing C#. I have to make a Winforms app that can convert between Celsius and Fahrenheit. I have all the code down (basically), but I need to have the result output to a label. I can send the answer to the label with lblOutput = Output.ToString();
but I need a message like "[input] Celsius will is [output] Fahrenheit". I tried putting in between the brackets after "ToString" but I was getting an error.
I have everything else coded except for this. I have been looking for the last couple days to figure it out, but I can't find an answer. Any help is greatly appreciated.
Edit:
The code that I have set up appears inside of a button. I have two radio buttons that saying that the value put into a text box is either Far -> Cels or Cels -> Far.
This is what I have coded in my button. If there is any way to improve upon it, please let me know.
private void btnConvert_Click(object sender, EventArgs e)
{
decimal Input = Convert.ToDecimal(txtInput.Text); //grabs the input that the user entered
if (rbtnCelsius.Checked == true) //Test to see if the Celsius radio button is checked
{
decimal Output = ((Input - 32) * 5) / 9; //If yes, it uses this formula to convert the input from Farenheit to Celsius
txtOutput.Text = Output.ToString(); //Outputs the message to the user, showing the Celsius end point
}
else //Says that the Celsius Radio Button is not checked, meaning that the Farenheit radio button is
{
decimal Output = (Input * 9) / 5 + 32; //Moves onto this formula, converts Celsius to Farenheit
txtOutput.Text = Output.ToString(); //outputs the message to the user, showing the Farenheit end point
}
//txtOutput.Text = Output.ToString();
//Commented out because it gives an error saying that "Output does not exist in current context"
}
Upvotes: 0
Views: 1660
Reputation: 9
I'm not sure what your question is about but if you need to format your output into a set phrase you can do this.
private void btnConvert_Click(object sender, EventArgs e)
{
decimal Input = Convert.ToDecimal(txtInput.Text); //grabs the input that the user entered
// declare this outside the loop so you can use it later.
decimal output = 0M;
if (rbtnCelsius.Checked == true) //Test to see if the Celsius radio button is checked
{
output= ((Input - 32) * 5) / 9; //If yes, it uses this formula to convert the input from Farenheit to Celsius
}
else //Says that the Celsius Radio Button is not checked, meaning that the Farenheit radio button is
{
output= (Input * 9) / 5 + 32; //Moves onto this formula, converts Celsius to Farenheit
}
txtOutput.Text =string.Format("{0} Celsius will is {1} Fahrenheit",input,output);
}`
p.s. Do remember to use a try parse method to ensure that the txtInput is definitely convertible to decimal.
Upvotes: 1
Reputation: 812
I don't know if you care about that, but for some cases you can get something like 12.243333333 by calling Output.ToString(), to make it more user-friendly, you can make it like this
Output.ToString("F2")
Which will change it to 12.24. You can use this together with string.Format like folks suggested.
Upvotes: 1
Reputation:
int fahrenheit, celsius;
// your code to set the two variables
string yourLabelText = String.Format("{0} Celcius is {1} Fahrenheit", celsius.ToString(), fahrenheit.ToString());
yourLabel.Text = yourLabelText;
Here is a reference for String.Format(). (thanks to Lukazoid!)
Upvotes: 2
Reputation: 16991
you can use string.format to replace tokens like {0} or {1} in a string, or you can directly use string concatenation. I'd like to avoid directly giving you the answer since you said this is homework, but if you look for c# examples of this you should be able to find your way.
Upvotes: 0