在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(而不是intdouble(。就我而言,我不知道你试图对taxInt变量做什么(按照你的编码方式,它永远是零(。相反,我做了一个合理的税收计算。

catch块下面的所有代码都在else块内,所以我不希望它执行。如果您希望在catch之后执行某个内容,请将其从else块中删除。

最新更新