在许多TextBox中汇总数字并将它们写入文件

时间:2016-06-10 22:30:07

标签: c# textbox file-writing

这个程序是关于插入有人在文本框中进行的费用,我必须只插入数字。所以我必须将文本框中的所有数字保存到txt文件中,但总结一下。你能帮我一些想法吗?

private void button2_Click_1(object sender, EventArgs e)
{
    try
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(textBox1.Text + " " + textBox2.Text + " " + textBox3.Text + " " + textBox4.Text);
        File.WriteAllText(fileName, sb.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

2 个答案:

答案 0 :(得分:1)

这是一种从TextBox中读出数字并将它们写入输出文件的强大方法:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // populate textboxes
                var boxs = new[] {textBox1, textBox2};

                // get user input
                var decimals = new List<decimal>();
                var expenses = GetExpenses(boxs, decimals);
                if (!expenses)
                    throw new InvalidOperationException("Expecting numbers");

                // write to file
                using (var stream = File.Create("output.txt"))
                using (var writer = new StreamWriter(stream))
                {
                    foreach (var d in decimals)
                    {
                        writer.WriteLine(d);
                    }

                    var total = decimals.Sum();
                    writer.WriteLine("Total: " + total);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private bool GetExpenses(TextBox[] boxs, List<decimal> list)
        {
            if (boxs == null) throw new ArgumentNullException(nameof(boxs));
            if (list == null) throw new ArgumentNullException(nameof(list));
            foreach (var box in boxs)
            {
                var text = box.Text;
                decimal result;
                if (!decimal.TryParse(text, out result))
                    return false;

                list.Add(result);
            }
            return true;
        }
    }
}

答案 1 :(得分:0)

你需要一行加上这样的数字:

private void button2_Click_1(object sender, EventArgs e)

{

try

{

StringBuilder sb = new StringBuilder();

sb.AppendLine(textBox1.Text + " " + textBox2.Text+ " " + textBox3.Text+ " " + textBox4.Text);

sb.AppendLine((Int32.Parse(textBox1.Text) + Int32.Parse(textBox2.Text) + Int32.Parse(textBox3.Text) + Int32.Parse(textBox3.Text)).ToString())

            File.WriteAllText(fileName, sb.ToString());

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
相关问题