Reputation: 3
I have this form to due for a school class i am in. Im having trouble running the program. Here are the requirements:
When the user clicks the button to convert, the first step is to validate the user input in the textbox. Any real number is acceptable. If the user input in the textbox is not a real number, display an appropriate message in the results output label as follows. “ERROR: Please enter a numeric temperature to convert.”
If the user input is a real number, store it, then calculate the converted temperature value based on the “convert to” radio button selected. Then display the results in the output label. Follow this example for the appropriate format: “72.4 degrees Fahrenheit converts to 22.44 degrees Celsius.”
When the user clicks the button to clear, the form should be reset to its default state, ready for an entry. Hint: there may be things to do other than just clearing both the input textbox and the results output label.
When the user clicks the button to exit, the form should close, ending the program
This is the code I have:
namespace TemperatureConversionForm
{
public partial class frmTemperatureConverter : Form
{
public frmTemperatureConverter()
{
InitializeComponent();
}
private void btnConvert_Click(object sender, EventArgs e)
{
double tempurature;
double celsius;
double fahrenheit;
if (!double.TryParse(txtInput.Text, out tempurature) == false )
{
MessageBox.Show("ERROR: Please enter a numeric temperature to convert");
txtInput.ResetText();
txtInput.Focus();
}
if (rdoCelsius.Checked == true)
{
celsius = double.Parse(txtInput.Text);
celsius = (5.0 / 9.0 * (txtInput - 32));
lblConversion.Text = celsius.ToString();
}
else if (rdoFahrenheit.Checked == true)
{
fahrenheit = double.Parse(txtInput.Text);
fahrenheit = (txtInput * 9.0 / 5.0 + 32);
lblConversion.Text = fahrenheit.ToString();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInput.ResetText();
lblConversion.ResetText();
txtInput.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I am getting 2 errors:
Code Description
CS0019 Operator '-' cannot be applied to operands of type 'TextBox' and 'Int'
CS0019 Operator '*' cannot be applied to operands of type 'TextBox' and 'double'
Upvotes: 0
Views: 3863
Reputation: 219037
Just as the compiler is telling you, you're trying to perform math on a TextBox
object:
txtInput - 32
It's likely that you meant to perform math on the value that you parsed from the TextBox
:
celsius = (5.0 / 9.0 * (celsius - 32));
and:
fahrenheit = (fahrenheit * 9.0 / 5.0 + 32);
Basically, a TextBox
is an object, not a value. It has properties and functionality and all sorts of things that objects have. While you may be tempted to conceptually think of it as representing the value that it holds, the language needs you to be more specific than that. You get the value from the TextBox
here:
celsius = double.Parse(txtInput.Text);
Then you need to perform calculations on that value itself, not on the TextBox
object from which it came.
Upvotes: 1