Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,

if i enterd values in textbox1 in form1 like below
1
2
3
4
i used textbox2 for sum of textbox1
textbox1 value not fix 4 numers it may be 7 or 10 whatever user enterd inputs

so i want to get help for your for display user enterd values with sum in other form2
like
1
2
3
4
----
10
----
plz help for code
Posted
Comments
coded007 25-Aug-14 2:02am    
Are you using any delimiters ?

1 solution

public class Form1 : Form
{
    private TextBox textBox1 = new TextBox();

    private Form2 form2 = new Form2();

    public Form1()
    {
        this.textBox1.Multiline = true;
        this.textBox1.Height = 100;
        this.textBox1.TextChanged += this.textBox1_TextChanged;

        this.Controls.Add(this.textBox1);
        this.Load += this.Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        form2.Show();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int sum = 0;

        foreach (string s in this.textBox1.Lines)
        {
            int v;

            if (int.TryParse(s, out v))
            {
                sum += v;
            }
        }

        form2.Sum = sum.ToString();
    }
}


C#
public class Form2 : Form
{
    private TextBox textBox2 = new TextBox();

    public Form2()
    {
        this.Controls.Add(this.textBox2);
    }

    public string Sum
    {
        set { this.textBox2.Text = value; }
    }
}
 
Share this answer
 

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