views:

586

answers:

6

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;
        }
    }
}
A: 

Perhaps try a different method:

private void AddDecimal()
{
    txtScreen.SelectionLength = txtScreen.TextLength;
    txtScreen.SelectedText += ".";
}

(Also is your text box, text aligment, right aligned... if not that may contribute to your problem.)

still does the same thing
Pops
@Pops - If my suggestions do not work then there must be an unwanted side effect somewhere else in your code. Do you have any event handlers on txtScreen? For example a KeyDown handler or some other such handler that moves the insertion point?
ive edited the question. it now contains my whole code
Pops
A: 

I think you have a few things here.

By the looks of it, you've set:

txtScreen.right to left = true;

If you append just the decimal point, you get the result you describe. Try using something like:

txtScreen.AppendText(".00");

This will give you the result you are describing.

You could be opening a can of worms. When you start formatting the textbox, you are changing it from holding a value to presentation. Eg:

decimal value = 567;
txtScreen.Text = value.ToString("0.00");

Then you will have to start writing crazy validation rules to avoid values like 567.00.1 etc.

Christian Payne
i will be disabling the decimal button so this cant happen. and yes it is right to left.
Pops
+1  A: 

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.

R. Bemrose
im using XP :-S
Pops
+1  A: 

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.

Julian Martin
ive set it to align via the gui interface (in the properties) not in the code. Does this make a difference?
Pops
No - it will have the same effect. The same code as you would type is generated by the Windows Forms Designer in the [FormName].Designer.cs file.
Julian Martin
Fyi. I am running Windows 7 with VS 2008 and have replicated your problem by setting RightToLeft to True. Turning off RightToLeft fixes it, for me at least.
Julian Martin
how do i turn it off?
Pops
+1  A: 

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

Kerido
Why not a `decimal` variable? No floating point rounding that way.
R. Bemrose
Whatever suits best
Kerido
A: 

Just to let you all know who are interested.

I managed to fix it somehow. All I did was delete the right to left thing in the design code and then realigned it (using the GUI) to right and its worked...odd as I did nothing different to last time...

Oh well

Thank you all for your help

Much aprreciated

x

Pops