Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void calctxt_KeyDown(object sender, KeyEventArgs e)
        {
            double Total;
            string[] numbers = calctxt.Text.Split('+');
            Total = 0;
            foreach(string value in numbers)
            {
                Total = Total + numbers(num)
            }


How would I complete this string to add all the numbers between the + signs?

What I have tried:

C#
private void calctxt_KeyDown(object sender, KeyEventArgs e)
        {
            double Total;
            string[] numbers = calctxt.Text.Split('+');
            Total = 0;
            foreach(string value in numbers)
            {
                Total = Total + numbers(num)
            }


How would I complete this string to add all the numbers between the + signs?
Posted
Updated 18-Oct-18 5:54am

Try something like this:

C#
using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine(Compute());
	}
	
	private static string Compute(){
		string message = string.Empty;
		bool isValid = true;
			double result = 0;
		    string sampleText = "2+5+1+6+3+2+1";
		    string delimtedValue = "+";
		
		    if(sampleText.Contains(delimtedValue)){
				string[] numbers = sampleText.Split(delimtedValue.ToCharArray());
				double val;

				foreach(string num in numbers)
				{
					if (!double.TryParse(num, out val))
					{
						message ="Unable to compute. String has invalid numeric values";
						isValid = false;
						break;

					}

					result = result + val;
				}
			}
		    
		if(!isValid)
			return message;
		else
			return string.Format("The total sum is: {0}", result);
	}
}
 
Share this answer
 
Comments
Member 14023534 18-Oct-18 13:17pm    
How do I change my form to Main?
Im only getting errors.
Vincent Maverick Durano 18-Oct-18 14:32pm    
The Main method is for console type of app. What you can do is just call the Method Compute() in your TextBox KeyDown event.
You need to convert the strings to numbers inside your loop, using double.TryParse[^]
double val;
if (!double.TryParse(value, out val))
   {
   ... report problem to user ...
   return;
   }
You can then add them into your total.
 
Share this answer
 
Comments
Member 14023534 17-Oct-18 16:58pm    
Here is my code, Please show me, I'm new at this and feel lost:
public partial class cardCalc : Form
    {
        
        public static string calcTextcopy;
        public cardCalc()
        {
            InitializeComponent();
        }
        public static string sendtext = "";
        private void calctxt_KeyDown(object sender, KeyEventArgs e)
        {
            double Total;
            string[] numbers = calctxt.Text.Split('+');
            Total = 0;
            foreach(string num in numbers)
            {
                Total = Total + numbers(num)
            }

            if (e.KeyCode == Keys.Enter)
            {
                if (calctxt.Text != "")
                {
                    sendtext = calctxt.Text;
                    CashUpForm frm = new CashUpForm();
                    frm.Show();
                    Application.Exit();
        
    }
                else
                {
                    Application.Exit();
                }
            }
            

        }
    }
}
Member 14023534 17-Oct-18 17:13pm    
its giving me an error under "value", but my code just looks wrong:
 private void calctxt_KeyDown(object sender, KeyEventArgs e)
        {
            double value;
            string[] numbers = calctxt.Text.Split('+');
            double num;
            double val;
            value = 0;
            if (!double.TryParse(value, out val))
            {

                return;
            }
Atlapure Ambrish 18-Oct-18 3:04am    
where is your loop??
Also, you are assigning 0 to value, instead it should be assigned to numbers item in loop and then parse.
OriginalGriff 18-Oct-18 3:18am    
When I said "inside your loop" why did you assume that that meant "throw the loop away, you don't need it"?
Member 14023534 19-Oct-18 15:01pm    
These are the three property functions of my textbox:
private void cardsTotalbox_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Add)
            {
                if (cardsTotalbox.Text != "")
                {
                    this.cardsTotalbox.Font = new Font("Microsoft Sans Serif", 12F);
                }
            }
            else if(e.KeyCode == Keys.Enter)
            {
                this.cardsTotalbox.Font = new Font("Microsoft Sans Serif", 9F);
                twoHundredrBox.Focus();
            }
        }

        public void onlynumwithsinglepoint(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.' || e.KeyChar == '+'))
            { e.Handled = true; }
            TextBox txtDecimal = sender as TextBox;
            
        }

        private void cardsTotalbox_KeyUp(object sender, KeyEventArgs e)
        {
            double Total;
            string[] numbers = cardsTotalbox.Text.Split('+');
            Total = 0;
            foreach (string value in numbers)
            {
                if (!string.IsNullOrEmpty(value))
                {
                    Total = Total + Convert.ToDouble(value);
                }
            }

            cardsTotalbox.Text = Total.ToString();
        }

The problem I'm having now is that if I press enter to activate the textbox, it immediately has a zero, then when I add a value and + another value, the zero changes to the sum of the two values, but I would like the entire string to show in the textbox with all the + signs before it calculates the value, not everytime I press the + sign.
C#
private void textBox1_KeyUp(object sender, KeyEventArgs e)
       {
           double Total;
           string[] numbers = textBox1.Text.Split('+');
           Total = 0;
           foreach (string value in numbers)
           {
               if (!string.IsNullOrEmpty(value))
               {
                   Total = Total + Convert.ToDouble(value);
               }
           }

           textBox2.Text = Total.ToString();
       }
 
Share this answer
 
Comments
Richard MacCutchan 18-Oct-18 12:32pm    
Total = Total + Convert.ToDouble(value);
What happens if the user types "3 + a" ?
Member 14023534 18-Oct-18 16:59pm    
I have a method in place for that.
Richard MacCutchan 19-Oct-18 3:08am    
I don't see it in the code above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900