Reputation: 1636
I have 2 foreach loops and both of them work by themselves (when the other is commented out).. but when I stick them together only the first one works....
// Splits the RichTextBox up so the numbers can be formatted properly.
String[] myXLines = calculateXRichTextBox.Text.Split('\n');
String[] myYLines = calculateYRichTextBox.Text.Split('\n');
// Converts the numbers to only contain 2 decimal places.
foreach (string decimalXLines in myXLines)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "\n");
foreach (string decimalYLines in myYLines)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "\n");
Does anyone know how to get this to work or why it's not working properly (the RTB does not append the text)?
Thanks in advance for any help.
private void calculateXAndYPlacementOne()
{
try
{
try
{
// Save the contents of the placementOneListBox into the file.
System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath + "\\Calculating X,Y File.txt");
foreach (object item in placementOneListBox.Items)
sw.WriteLine(item.ToString());
sw.Close();
}
// Catches an exception if the file was not saved.
catch (Exception)
{
MessageBox.Show("Could not write to file.");
}
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath + "\\Calculating X,Y File.txt");
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
var fileLines = "";
while ((fileLines = fileReader.ReadLine()) != null)
fileList.Add(fileLines);
// 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 }));
// Add the results to the lists.
xResult.Add(xValue);
yResult.Add(yValue);
// Store the old X and Y values.
oldXRichTextBox.AppendText(xValue + "\n");
oldYRichTextBox.AppendText(yValue + "\n");
try
{
// Calculate the X & Y values (including the x & y displacements)
double doubleX = double.Parse(xValue);
double doubleXValue = double.Parse(xDisplacementTextBox.Text);
StringBuilder sbX = new StringBuilder();
sbX.AppendLine((doubleX + doubleXValue).ToString());
double doubleY = double.Parse(yValue);
double doubleYValue = double.Parse(yDisplacementTextBox.Text);
StringBuilder sbY = new StringBuilder();
sbY.AppendLine((doubleY + doubleYValue).ToString());
calculateXRichTextBox.AppendText(sbX + "");
calculateYRichTextBox.AppendText(sbY + "");
// Removes the blank lines.
calculateXRichTextBox.Text = Regex.Replace(calculateXRichTextBox.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
calculateYRichTextBox.Text = Regex.Replace(calculateYRichTextBox.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
}
// Catches if it fails
catch (Exception)
{
MessageBox.Show("Could not calculate the X & Y values.");
}
}
});
// Splits the RichTextBox up so the numbers can be formatted properly.
String[] myXLines = calculateXRichTextBox.Text.Split('\n');
String[] myYLines = calculateYRichTextBox.Text.Split('\n');
foreach (string decimalXLines in myXLines)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "\n");
foreach (string decimalYLines in myYLines)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "\n");
for (int theLine = 0; theLine < placementOneListBox.Items.Count; theLine++)
{
string replacement1 = calculateXRichTextBox.Lines[theLine];
while (replacement1.Length < 7)
replacement1 = " " + replacement1;
placementOneListBox.Items[theLine] = ((string)placementOneListBox.Items[theLine]).Remove(20, 7).Insert(20, replacement1);
string replacement2 = calculateYRichTextBox.Lines[theLine];
while (replacement2.Length < 7)
replacement2 = " " + replacement2;
placementOneListBox.Items[theLine] = ((string)placementOneListBox.Items[theLine]).Remove(29, 7).Insert(29, replacement2);
};
}
catch (Exception)
{
MessageBox.Show("Could not manipulate the data properly.");
}
File.Delete(filePath + "\\Calculating X,Y File.txt");
}
Upvotes: 0
Views: 2799
Reputation: 1636
// Splits the RichTextBox up so the numbers can be formatted properly.
String[] myXLines = calculateXRichTextBox.Text.Split('\n');
String[] myYLines = calculateYRichTextBox.Text.Split('\n');
int counterX = 0;
int counterY = 0;
foreach (string decimalXLines in myXLines)
{
if (counterX == 0)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "");
if (decimalXLines != "" && counterX != 0)
{
removedXDecimalRichTextBox.AppendText("\n");
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "");
}
counterX++;
}
foreach (string decimalYLines in myYLines)
{
if (counterY == 0)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "");
if (decimalYLines != "" && counterY != 0)
{
removedYDecimalRichTextBox.AppendText("\n");
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "");
}
counterY++;
}
Upvotes: 0
Reputation: 5833
The problem in the input data, each time when an input string is not a correct number the call of the Math.Round(Convert.ToDouble(decimalXLines), 2) will throw exception. If that code working in the background thread you will not receive critical exception.
In my mind the last element of the String[] myXLines = calculateXRichTextBox.Text.Split('\n'); is a empty string.
You can try to add the StringSplitOptions.RemoveEmptyEntries into split code:
String[] myXLines = calculateXRichTextBox.Text.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries);
but this is workaround, not a 100% woring solution.
Upvotes: 1
Reputation: 1502
What happens if you break each one out into multiple statements? You're doing a Convert.ToDouble() and a Round() all inline with the AppendText() call. Are you sure each step is behaving correctly?
Upvotes: 1