decimal place button in calculator program
Hi all
I am new to C# and programming and having a problem with a button on my calculator program. Its the decimal place button. I would like it so that no many how times I press the button it only gives one decimal place. Becuase if you press it twice, you get a number like 1..2 or 2000..3 and obviously the calculator can't process it. The code I have written is:
private void decimalPlace_Click(object sender, EventArgs e)
{
calcDisplay.Text = calcDisplay.Text + decimalPlace.Text;
}
where calcDisplay is the textbox where the calculator displays its numbers and decimalPlace is the name of the button with the decimal place in it.
Many thanks
Re: decimal place button in calculator program
Hi,
Welcome to the forum. The lesson here is to use flow control statements to get different behaviors given different states of the program. In particular we will use if:
Code:
private void decimalPlace_Click(object sender, EventArgs e)
{
if( !calcDisplay.Text.Contains(".") )
{
calcDisplay.Text = calcDisplay.Text + ".";
}
}
Here we call a method, calcDisplay.Text.Contains(".") (MSDN: link) which returns true if calcDisplay.Text already contains a decimal. Then we invert this result using the ! to perform an action when calcDisplay.Text does not contain a decimal already.
Does that make sense?
Your question indicates a deep unfamiliarity with programming techniques. This is not necessarily a bad thing (we all started out as beginners), but I think you would strongly benefit from going through some tutorials. This one ought to get you started: link. You can Google for more.
Also, you will be more likely to get helpful answers to your questions if you use [code] and [/code] tags around your code.