Decimal point in calculator c#

12,658

Solution 1

My advice is to set TextAlign to Right, but leave RightToLeft set to No.

Edit: Having said that, this issue may be unrelated to these settings.

I remember a friend having this a bug similar to this back in early 2009 in Visual Studio 2008 on Windows Vista. Strangely enough, the same problem did not occur on the same version of Visual Studio on Windows XP.

If you haven't updated Visual Studio / .NET 3.5 to Service Pack 1, I suggest doing that and seeing if it fixes the problem.

Solution 2

The RightToLeft looks to be your problem. As described in MSDN,

The RightToLeft property is used for international applications where the language is written from right to left, such as Hebrew or Arabic. When this property is set to RightToLeft..::.Yes, control elements that include text are displayed from right to left.

As one ofthe previous answers suggested, this should be set to false, but with TextAlign set to Right to mimic the appearance of a real calculator.

Solution 3

My advice is -- define a business layer. In your case -- a double variable. Upon button clicks, update the variable first. Then format the value.

Share:
12,658
Pops
Author by

Pops

Updated on June 25, 2022

Comments

  • Pops
    Pops almost 2 years

    AHHHHH ok this is driving me nuts.

    Why when does my decimal point in the wrong place e.g.

    if i have the string 567 in the textbox and click the decimal button i would expect (or i want) the textbox to change to 567. but instead i get .567

    It only goes into the correct place when i add another number e.g. if i had the number 4 then straight after doing the above I'd get 567.4

    Edit:

    Heres my whole code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace Calculator
    {
        public partial class frmCurrencyCalc : Form
        {
            public frmCurrencyCalc()
            {
                InitializeComponent();
            }
    
            private void cmdZero_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "0";
                }
                else
                {
                    txtScreen.AppendText("0");
                }
            }
            private void cmd1_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "1";
                }
                else
                {
                    txtScreen.AppendText("1");
                }
            }
    
            private void cmdTwo_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "2";
                }
                else
                {
                    txtScreen.AppendText("2");
                }
            }
    
            private void cmdThree_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "3";
                }
                else
                {
                    txtScreen.AppendText("3");
                }
            }
    
            private void cmdFour_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "4";
                }
                else
                {
                    txtScreen.AppendText("4");
                }
            }
    
            private void cmdFive_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "5";
                }
                else
                {
                    txtScreen.AppendText("5");
                }
            }
    
            private void cmdSix_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "6";
                }
                else
                {
                    txtScreen.AppendText("6");
                }
            }
    
            private void cmdSeven_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "7";
                }
                else
                {
                    txtScreen.AppendText("7");
                }
            }
    
            private void cmdEight_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "8";
                }
                else
                {
                    txtScreen.AppendText("8");
                }
            }
    
            private void cmdNine_Click(object sender, EventArgs e)
            {
                if (txtScreen.Text == "0")
                {
                    txtScreen.Text = "9";
                }
                else
                {
                    txtScreen.AppendText("9");
                }
            }
    
           private void cmdDecimal_Click(object sender, EventArgs e)
          {
              txtScreen.AppendText(".");
              cmdDecimal.Enabled = false;
          }
    
    
            private void cmdCancel_Click(object sender, EventArgs e)
            {
                txtScreen.Text = "0";
                cmdDecimal.Enabled = true;
            }
        }
    }