VitzzViperzz
Well-known member
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:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Let me know where you think the problem is.
Thanks
	
		
			
		
		
	
				
			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: 
			
		
	
								
								
									
	
		
			
		
		
	
	
	
		
			
		
		
	
								
							
							 
	 
 
		
 
 
		
 
 
		 
 
		