My Calculator numbers are being entered in reverse.

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

I building this calculator in a Windows Form environment and I have run into a big issue! The issue that I am facing is that it enters the numbers in reverse. So if you want to enter 7 and 5. It enters 5 and 7.

I think this is mostly down to the cursor being focused right after the user has clicked the button.

There is a function called InsertTextvalue() that is where I think the problem is occurring. But have a look at the code:

C#:
namespace Calculator
{
    /// <summary>
    /// A basic calculator
    /// </summary>


    public partial class Form1 : Form
    {
        #region Constructor     
        /// <summary>
        /// Default constructor
        /// </summary>


        public Form1()
        {
            InitializeComponent();
        }


        #endregion


        #region Clearing Buttons


        /// <summary>
        /// This section is about clearing the textbox. 
        /// </summary>
        /// <param name="sender">The event sender</param>
        /// <param name="e">The event arguments</param>


        private void CEButton_Click(object sender, EventArgs e)
        {
            // This line of code deletes the entire textbox.
            this.UserInputText.Text = string.Empty;
            FocusInputText();
        }


        private void DelButton_Click(object sender, EventArgs e)
        {


            // delete the value after th selected selection
            DeleteTextValue();


            
            FocusInputText();
        }


        #endregion


        #region Operator Methods


        private void DivideButton_Click_1(object sender, EventArgs e)
        {
            InsertTextValue("/");
            FocusInputText();
        }


        private void TimesButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("*");
            FocusInputText();
        }


        private void MinusButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("-");
            FocusInputText();
        }


        private void PlusButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("+");
            FocusInputText();
        }


        private void EqualsButton_Click(object sender, EventArgs e)
        {
            CalculateEquation();
            FocusInputText();
        }


        #endregion


        #region Number Operators


        private void SevenButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("7");
            FocusInputText();
        }


        private void EightButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("8");
            FocusInputText();
        }


        private void NineButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("9");
            FocusInputText();
        }


        private void FourButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("4");
            FocusInputText();
        }


        private void FiveButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("5");
            FocusInputText();
        }


        private void SixButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("6");
            FocusInputText();
        }


        private void OneButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("1");
            FocusInputText();
        }


        private void TwoButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("2");
            FocusInputText();
        }


        private void ThreeButton_Click(object sender, EventArgs e)
        {
            InsertTextValue("3");
            FocusInputText();
        }


        private void ZeroButton_Click(object sender, EventArgs e)
        {
            // The value of the left side is set to the value of the right side. 
            InsertTextValue("0");
            FocusInputText();
        }


        private void DotButton_Click(object sender, EventArgs e)
        {
            InsertTextValue(".");
            FocusInputText();
        }


        #endregion




        #region Calculating The Equation
        /// <summary>
        /// Calculates the given equations and out puts the answer. 
        /// </summary>
        private void CalculateEquation()
        {
            FocusInputText();
        }


        #endregion


        #region Private Helpers


        /// <summary>
        /// Focuses the input user text.
        /// </summary>
        private void FocusInputText()
        {
            this.UserInputText.Focus();
        }


        #endregion


        #region Insert Text Value
        /// <summary>
        /// Inserts the given text into the user input text box
        /// </summary>
        /// <param name="value"> The value to insert</param>
        private void InsertTextValue(string value)
        {
            var selectionStart = this.UserInputText.SelectionStart;


            this.UserInputText.Text = this.UserInputText.Text.Insert(this.UserInputText.SelectionStart, value);




            this.UserInputText.SelectionStart = selectionStart + value.Length;


            this.UserInputText.SelectionStart = 0;
        }
        #endregion




        #region DeleteTextValue


        private void DeleteTextValue()
        {
            // if we dont have a value to delete, return nothing. 
            if (this.UserInputText.Text.Length < this.UserInputText.SelectionStart + 1)
                return;


            var selectionStart = this.UserInputText.SelectionStart;


            // Delete the character to the right of this selection
 
            this.UserInputText.Text = this.UserInputText.Text.Remove(this.UserInputText.SelectionStart, 1);


            this.UserInputText.SelectionStart = selectionStart;
             
            this.UserInputText.SelectionStart = 0;
        }


        #endregion
    }
}

Let me know where you think the problem is.

Thanks
 
Last edited:
Please post only the relevant code. If you can't narrow it down to less than everything then I'll wager it's because you haven't done any actual debugging. Do you know how to debug, using breakpoints and stepping through the code? If not then now would be a very good time to learn. You can look at the state of your app as each and every line is executed and you can then identify EXACTLY when the actual state differs from the expected state. Not only that, you can see what data is in use at the time, which is often of critical importance.
 
I don't think that breakpoints would be useful here. We are not trying to trace where the data is going. We are looking to see why the cursor is putting itself before the number that it enters.

Let me show you this:

calc.PNG

Here is pressed 57 and it shows 75
 
Have a look at your InsertTextValue method, especially the use of SelectionStart.
 
I don't think that breakpoints would be useful here. We are not trying to trace where the data is going. We are looking to see why the cursor is putting itself before the number that it enters.
And debugging properly would have shown you that. Don't ever assume that debugging won't help.
 
Back
Top Bottom