我正在尝试从文件中检索数据以更改它并替换旧数据。
我有一个看起来像这样的文件:
TEXT NEXT_TEXT 10.505 -174.994 0
TEXT NEXT_TEXT 100.005 174.994 90
TEXT NEXT_TEXT -10.000 -5.555 180
TEXT NEXT_TEXT -500.987 5.123 270
TEXT NEXT_TEXT 987.123 1.000 180
TEXT NEXT_TEXT 234.567 200.999 90
我正在尝试获取第3 列和第 4 列以根据用户输入的内容更改数据TextBoxes
。让我们将第 3 列 标记为“X”,将第 4 列标记为 “Y”。有了这个,还有两个标记为"xTextBox"和"yTextBox"的文本框。这些文本框允许用户输入数字,并且他们为每个文本框(X 和 Y)输入的数字(正数、负数和/或小数点后 3 位)将添加到“X”列值和“Y”列值。
现在,我正在使用此代码(如下)去除“X”和“Y”列,并将它们的值显示到不同的RichTextBoxes
标记xRichTextBox和yRichTextBox中。
所以,xRichTextBox看起来像这样:
10.505
100.005
-10.000
-500.987
987.123
234.567
yRichTextBox看起来像这样:
-174.994
174.994
-5.555
5-123
1.000
200.999
代码:
private void calculateXAndYPlacementTwo()
{
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath);
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
while (true)
{
var line = fileReader.ReadLine();
if (line == null)
break;
fileList.Add(line);
}
// Creates new lists to hold certain matches for each list.
var xyResult = new List<string>();
var xResult = new List<string>();
var yResult = new List<string>();
// Iterate over each line in the file and extract the x and y values
fileList.ForEach(line =>
{
Match xyMatch = Regex.Match(line, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
if (xyMatch.Success)
{
// grab the x and y values from the regular expression match
String xValue = xyMatch.Groups["x"].Value;
String yValue = xyMatch.Groups["y"].Value;
// add these two values, separated by a space, to the "xyResult" list.
xyResult.Add(String.Join(" ", new[]{ xValue, yValue }));
// Adds the values into the xResult and yResult lists.
xResult.Add(xValue);
yResult.Add(yValue);
// Place the 'X' and 'Y' values into the proper RTB.
xRichTextBox.AppendText(xValue + "\n");
yRichTextBox.AppendText(yValue + "\n");
}
});
}
因此,例如,如果用户输入“10.005”和“xTextBox
-20 ”,yTextBox
则更新 xRichTextBox
将如下所示:
20.510
110.010
0.005
-490.982
997.128
224.572
更新 后的yRichTextBox
样子是这样的:
-194.994
154.994
-25.555
-14.877
-19.000
180.999
在这发生之后,我试图用更新的值替换原始文件的第 3 列和第 4 列。
- 我目前正在文件中获取值(“X”和“Y”)并将它们输出到单独
RichTextBoxes
的,但我不知道如何将这些值添加到从每个输入的值中TextBox
......我该怎么做? - 在
RichTextBox
使用“TextBox”值计算值后,如何获取这些新值并将它们添加到原始文件中?
**这些值并不总是相同的,否则我会将它们硬编码。
因此,新文件将如下所示:
TEXT NEXT_TEXT 20.510 -194.994 0
TEXT NEXT_TEXT 110.010 154.994 90
TEXT NEXT_TEXT 0.005 -25.555 180
TEXT NEXT_TEXT -490.982 -14.877 270
TEXT NEXT_TEXT 997.128 -19.000 180
TEXT NEXT_TEXT 224.572 180.999 90