将许多 TextBox 中的数字相加并将它们写入文件
Summing numbers in many TextBox and writing them to a file
这个程序是关于插入某人正在制作的费用,所以在文本框中我必须只插入数字。所以我必须将所有这些数字从文本框中保存到一个 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);
}
}
您需要一行将数字相加,如下所示:
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);
}
}
这是一种从文本框中读取数字并将其写入输出文件的可靠方法:
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;
}
}
}
这个程序是关于插入某人正在制作的费用,所以在文本框中我必须只插入数字。所以我必须将所有这些数字从文本框中保存到一个 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);
}
}
您需要一行将数字相加,如下所示:
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);
}
}
这是一种从文本框中读取数字并将其写入输出文件的可靠方法:
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;
}
}
}