Sorry my title is a bit vague, but what I’m trying to do will probably take a paragraph to explain.
At the moment what I’m trying to do is this:
- Gather user in put a from a text box
- Divide the value entered by 2
- Display this new value on a label
So, to get user values I’ve done this:
private void userLengthTextBox_TextChanged(object sender, EventArgs e)
{
_userLength = userLengthTextBox.Text;
}
This is coming in as a string. Then in a new function, I’m converting this string to a single (need it to be a float), calculating half the value entered and then storing it in a new float.
Like so:
_oldTlX = Convert.ToSingle(_userLength);
_tlX = _oldTlX / 2;
Then, when I press a button, I’m trying to get this value to display in a label:
_valueLabel.Text = Convert.ToString(_tlX);
However, when I press the button, the value remains at 0. No matter what I put. As far as I can see I’ve converted the value I need to the correct format needed, but it is still saying I’m doing it wrong.
I recommend you to use double internally, and then use a format string to show the result in a propper way:
You can also check at the MSDN how to format numeric results.
Hope it helps.