2016-06-10 3 views
0

Эта программа посвящена вложению расходов, которые кто-то делает в текстовых ящиках, я должен вставлять только числа. Поэтому я должен сохранить все эти числа из текстовых полей в текстовый файл, но суммировать. Можете ли вы помочь мне с некоторыми идеями?Подведение чисел во многих TextBox и запись их в файл

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); 
    } 
} 

ответ

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); 
     } 
    } 
1

Вот надежный способ чтения из числа от TextBoxes и записать их в выходной файл:

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; 
     } 
    } 
}