c# - 用 C# 编写程序,根据 Id (txtBxNumber),它将更新或在文本文件和富文本框中创建新记录
问题描述
fileName = txtBxFileNamePath.Text;
if (File.Exists(fileName))
{
if (txtBxDate.Text != null && txtBxNumber.Text != null && txtBxUnit.Text != null && txtBxUnitPrice.Text != null && txtBxShipTo.Text != null
&& txtBxOrdered.Text != null && richTxBxDesc.Text != null)
{
try
{
int higherThanZero = Int32.Parse(txtBxNumber.Text);
if (higherThanZero > 0)
{
using (StreamReader reader = File.OpenText(fileName))
{
string[] lines = File.ReadAllLines(fileName);
for (int i = 0; i < lines.Length - 1; i++)
{
string firstNum = lines[i].Substring(0, 2);
if (firstNum == txtBxNumber.Text)
{
string record = "hello ";
lines[i].Replace(lines[i], record);
}
else
{
int orderNum = Int32.Parse(txtBxOrdered.Text);
int unitPriceNum = Int32.Parse(txtBxUnitPrice.Text);
double tax = .13;
int taxInt = (int)tax;
int amount = orderNum * unitPriceNum;
string amountStr = amount.ToString();
int amountTotal = amount * taxInt;
string amountTotalStr = amountTotal.ToString();
amountList.Add(amountStr);
amountTotalList.Add(amountTotalStr);
string record = amountTotalStr.PadRight(30) + amountStr.PadRight(30);
richTxtBxRecord.Text += record + "\n";
using (StreamWriter write = new StreamWriter(fileName, true))
{
write.WriteLine(record + "\n");
write.Close();
}
}
}
}
}
else
{
richTxtBxError.Text += "Textbox Number must contain a digit higher than 0 ";
}
}
catch
{
richTxtBxError.Text += "Please make sure number text box is a digit";
}
}
else
{
richTxtBxError.Text += "please make sure that no text boxes are empty";
}
}
else
{
richTxtBxError.Text += "Please select a file that already exists";
}
我遇到了一个问题,一旦我通过 try-catch 语句“请确保数字是数字,没有代码执行。我正在尝试获取文本文件中的前几个字符并将其与用户输入匹配。如果输入与文本文件中已插入的内容相同,我更新整个记录。如果没有匹配项(不存在的数字),我将写入一个全新的记录。
解决方案
我不能完全按照你的逻辑,但我试过了。您应该能够获取此代码并做您想做的事情(无论它是什么)。
我首先声明了一些类级别的变量。
private DateTime _dateValue;
private int _numberValue;
private decimal _unitPrice;
private int _numberOrdered;
然后,由于您有这么多的先决条件和这么多的文本框,我将这些变量的验证和设置分解出来。它使逻辑(无论它应该是什么)更容易遵循:
private bool ValidateUserEntry()
{
bool isError = false;
if (!File.Exists(txtBxFileNamePath.Text))
{
AddError("File Name must exist");
isError = true;
}
if (txtBxDate.Text == string.Empty || !DateTime.TryParse(txtBxDate.Text, out var _dateValue))
{
AddError("The date must be a valid date");
isError = true;
}
if (txtBxNumber.Text == string.Empty || !int.TryParse(txtBxNumber.Text, out _numberValue) ||
_numberValue <= 0)
{
AddError("You must enter a number greater than 0 for [Number]");
isError = true;
}
if (txtBxUnitPrice.Text == string.Empty || !decimal.TryParse(txtBxUnitPrice.Text, out _unitPrice) ||
_unitPrice <= 0.0m)
{
AddError("The unit price must be a positive decimal number");
isError = true;
}
if (txtBxShipTo.Text == string.Empty)
{
AddError("A ship to address is required");
isError = true;
}
if (txtBxOrdered.Text == string.Empty || !int.TryParse(txtBxOrdered.Text, out _numberOrdered) ||
_numberOrdered <= 0)
{
AddError("The Number ordered must be a number greater than 0");
isError = true;
}
if (richTxBxDesc.Text == string.Empty)
{
AddError("A description is required");
isError = true;
}
return !isError;
}
我还添加了两个实用函数来管理错误列表:
private void ClearError()
{
richTxtBxError.Text = string.Empty;
}
private void AddError(string errorMessage)
{
richTxtBxError.Text += (errorMessage + Environment.NewLine);
richTxtBxError.SelectionStart = richTxtBxError.Text.Length;
richTxtBxError.SelectionLength = 0;
}
现在是真正的代码。据我所知,您想扫描一个文本文件。如果前几个字符位置的数字与输入中的数字匹配,则将行更改为一些常量文本。否则,您想进行计算并将计算结果放在文本行上。
我的输入文件如下所示:
1 First
2 Second
3 Third
12 Twelth
13 Thirteenth
34 Thirty-fourth
我运行的代码如下所示。逻辑没有意义,但这是我可以从您的代码中辨别出来的。我没有尝试对文件进行即时处理(除非您非常小心,否则效果永远不会很好),而是将输出收集到List<string>
. 获得所有输出后,我将其放入文本框控件并覆盖文件。
ClearError();
//check pre-conditions
if (!ValidateUserEntry())
{
return;
}
string[] lines;
using (StreamReader reader = File.OpenText(txtBxFileNamePath.Text))
{
lines = File.ReadAllLines(txtBxFileNamePath.Text);
}
List<string> newLines = new List<string>();
for (var lineIndex = 0; lineIndex < lines.Length; ++lineIndex)
{
var line = lines[lineIndex];
if (line.Length > 2 && int.TryParse(line.Substring(0, 2), out var linePrefixNumber) &&
linePrefixNumber == _numberValue)
{
newLines.Add("Bingo, hit the right record");
}
else
{
decimal tax = .13m;
var amount = _numberOrdered * _unitPrice;
var amountTotal = amount * (1m + tax);
//amountList.Add(amount.TosString());
//amountTotalList.Add(amountTotal.ToString());
var newRecord = $"{amountTotal,30:C}{amount,30:C}";
newLines.Add(newRecord); //every record but one will be the same, but, such is life
}
}
//at this point, the newLines list has what I want
//put it in the text box
richTxtBxRecord.Text = string.Join(Environment.NewLine, newLines);
//and write it out
using (StreamWriter write = new StreamWriter(txtBxFileNamePath.Text, append:false))
{
write.Write(richTxtBxRecord.Text);
write.Flush();
}
输入看起来像:
Number: 12
Number Ordered: 3
Unit Price: 1.23
输出(奇怪的是 - 但这是我可以从你的代码中得出的)看起来像:
$4.17 $3.69
$4.17 $3.69
$4.17 $3.69
Bingo, hit the right record
$4.17 $3.69
$4.17 $3.69
您可以看到开头为 12 的输入行被切换为宾果游戏。其余的得到相同的信息。我确定这不是你想要的。但是,使用此代码,您应该能够得到您想要的东西。
另请注意,我将所有货币值视为decimal
(不是int
或double
)。在我的一生中,我不知道你试图用这个taxInt
变量做什么(它永远是你编码它的方式为零)。相反,我做了一个合理的税收计算。
推荐阅读
- java - 未从字典中返回有效单词
- auth0 - Auth0 无密码到新电子邮件地址
- c# - 自定义列上的一对多关系
- html - Flexbox:在移动设备上将 2 列折叠为单列,但重新排列元素
- python - 不明白这个 TypeError 的原因:'dict' object is not callable
- powershell - 如何在 azure powershell 脚本任务中提供 json 请求正文
- javascript - 通过firebase函数调度谷歌云任务时出错,但从任务仪表板调度时没有错误
- ios - 屏幕顶部的额外空间
- python - 在 Pycharm 中禁用自动清除控制台(运行窗口)
- javascript - 在 GET 请求依赖于 POST 请求的情况下发出 http(s) 请求