【发布时间】:2011-07-26 20:57:48
【问题描述】:
如果我有一个从包含以下内容的文件加载的RichTextBox:
TEXT MORETEXT 10.505 100.994 0
TEXT MORETEXT -5.132 -12.994 90
TEXT MORETEXT 100.001 -8.994 270
和一个TextBox,其中包含用户在文本框中输入的任何内容。假设用户输入 "10.005"。
我的问题是,我如何获取这个值并将其添加到包含值 10.505 的 3rd 列,- 5.132, 100.001。添加后,我想取值并替换字符串中的旧值。 所以更新后的RichTextBox 看起来像这样。
TEXT MORETEXT 20.510 100.994 0
TEXT MORETEXT 4.873 -12.994 90
TEXT MORETEXT 110.006 -8.994 270
马上我可以使用以下代码从RichTextBox 中删除字符串:
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");
}
});
}
要获取 xRichTextBox 中的值,如下所示:
10.505
-5.132
100.001
yRichTextBox 看起来像:
100.994
-12.994
-8.994
但我不知道如何将它们转换为可以在其上使用 addition 的值...
编辑: 我已经搞砸了一些......我现在正在使用这段代码(如下)来尝试完成我需要它做的事情。这仅适用于 "X" (第 3 列)。
但是此代码不起作用(它将用户输入连接到 xRichTextBox 的末尾,而不是在数学上将其添加到每一行......)
xDisplacementTextBox 是用户输入,xRichTextBox 是从主字符串中剥离的值。
StringBuilder stringBuilder = new StringBuilder();
string[] Lines = xRichTextBox.Text.Split('\n');
double d = double.Parse(xDisplacementTextBox.Text);
for(int i = 0; i < Lines.Length; ++i)
{
string newThing = double.Parse((Lines[i]) + d).ToString();
stringBuilder.AppendLine(newThing);
}
xRichTextBox.Text = stringBuilder.ToString();
这也不允许我输入带有小数的值(即 50.005)..
【问题讨论】: