decimal place button in calculator program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: decimal place button in calculator program

  1. #1
    Join Date
    Jun 2011
    Posts
    3

    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

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    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.
    Last edited by BioPhysEngr; July 17th, 2011 at 10:09 PM. Reason: missed a ;
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center