C# - Replace part of string from ListBox with RichTextbox Data
I have a listBox labeled placementTwoListBox
where I have data loaded into it. The placementTwoListBox looks like this:
placementTwoListBox
:
U3 IC-00276G 236.135 198.644 90
U12 IC-00270G 250.610 201.594 0
J1 INT-00112G 269.665 179.894 180
J2 INT-00112G 269.665 198.144 180
J6 INT-00113G 227.905 174.994 180
J3 INT-00113G 227.905 203.244 180
U13 EXCLUDES 242.210 181.294 180
I also have TWO richTextBoxes labeled calculatedXRichTextBox
and calculatedYRichTextBox
. They look li开发者_StackOverflow社区ke this:
calculatedXRichTextBox
:
246.135
260.610
279.665
279.665
237.905
237.905
252.210
calculatedYRichTextBox
:
298.644
301.594
279.894
298.144
274.994
303.244
281.294
I am trying to replace the values in the 3rd column of placementTwoListBox
with the values in calculatedXRichTextBox
and replace the values in the 4th column of placementTwoListBox
with the values in calculatedYRichTextBox
to get the final result (and placing it back into the original ListBox) of:
placementTwoListBox
(updated):
U3 IC-00276G 246.135 298.644 90
U12 IC-00270G 260.610 301.594 0
J1 INT-00112G 279.665 279.894 180
J2 INT-00112G 279.665 298.144 180
J6 INT-00113G 237.905 274.994 180
J3 INT-00113G 237.905 303.244 180
U13 EXCLUDES 252.210 281.294 180
- Does anyone know how to implement this?
EDIT:
When using these two: @CrazyDart
calculatedXRichTextBox
:
758.135
772.61
791.665
791.665
749.905
749.905
764.21
calculatedYRichTextBox
:
-301.356
-298.406
-320.106
-301.856
-325.006
-296.756
-318.706
The placementTwoListBox
turns out to look like this:
U3 IC-00276G 758.135 -301.356666666 90
U12 IC-00270G 772.61 -298.40666666 0
J1 INT-00112G 791.665 -320.1066666 180
J2 INT-00112G 791.665 -301.856666 180
J6 INT-00113G 749.905 -325.00666 180
J3 INT-00113G 749.905 -296.7566 180
U13 EXCLUDES 764.21 -318.706 180
This should get you started:
string[] lines = placementTwoListBox.Lines;
for (int line = 0; line < lines.Length; line++)
{
string replacement1 = calculatedXRichTextBox.Lines[line];
while (replacement1.Length < 7)
{
replacement1 = " " + replacement1;
}
lines[line] = lines[line].Remove(20, 7).Insert(20, replacement1);
string replacement2 = calculatedYRichTextBox.Lines[line];
while (replacement2.Length < 7)
{
replacement2 = " " + replacement2;
}
lines[line] = lines[line].Remove(29, 7).Insert(29, replacement1);
}
placementTwoListBox.Lines = lines;
UPDATE: I totally missed that some are lists, not all RichTextBoxes... see updated code:
for (int line = 0; line < placementTwoListBox.Items.Count; line++)
{
string replacement1 = calculatedXRichTextBox.Lines[line];
while (replacement1.Length < 7)
{
replacement1 = " " + replacement1;
}
placementTwoListBox.Items[line] = ((string)placementTwoListBox.Items[line]).Remove(20, 7).Insert(20, replacement1);
string replacement2 = calculatedYRichTextBox.Lines[line];
while (replacement2.Length < 7)
{
replacement2 = " " + replacement2;
}
placementTwoListBox.Items[line] = ((string)placementTwoListBox.Items[line]).Remove(29, 7).Insert(29, replacement1);
}
This code is to fill the calculatedXRichTextBox
and calculatedYRichTextBox
from the placementTwoListBox
:
string[] lines = new string[placementTwoListBox.Items.Count];
for (int itemIndex = 0; itemIndex < lines.Length; itemIndex++)
{
lines[itemIndex] = placementTwoListBox.Items[itemIndex].ToString();
}
List<string> calculatedXLines = new List<string>();//to fill calculatedXRichTextBox
List<string> calculatedYLines = new List<string>();//to fill calculatedYRichTextBox
foreach (string line in lines)
{
string[] items = line.Split(new string[] { "\t", " " },
StringSplitOptions.RemoveEmptyEntires);
if (items.Length > 2)
{
calculatedXLines.Add(items[2]);
calculatedYLines.Add(items[3]);
}
}
//add them to the calculatedYRichTextBox and the placementTwoListBox
calculatedXRichTextBox.Text = string.Join("\n", calculatedXLines);
calculatedYRichTextBox.Text = string.Join("\n", calculatedYLines);
Edit: Answering the question:
string[] calculatedXLines = calculatedXRichTextBox.Text.Split(new string[] { "\n", "\r" },
StringSplitOptions.RemoveEmptyEntries);
string[] calculatedYLines = calculatedYRichTextBox.Text.Split(new string[] { "\n", "\r" },
StringSplitOptions.RemoveEmptyEntries);
string[] lines = new string[placementTwoListBox.Items.Count];
for (int itemIndex = 0; itemIndex < lines.Length; itemIndex++)
{
lines[itemIndex] = placementTwoListBox.Items[itemIndex].ToString();
}
List<string[]> newLines = new List<string[]>();
for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
{
string[] items = lines[lineIndex].Split(new string[] { " ", "\t" },
StringSplitOptions.RemoveEmptyEntries);
items[2] = calculatedXLines[lineIndex];
items[3] = calculatedYLines[lineIndex];
newLines.Add(items);
}
//represents your data on the `placementTwoListBox`:
placementTwoListBox.Items.Clear();
foreach (string[] lineItems in newLines)
{
placementTwoListBox.Items.Add(string.Join("\t", lineItems));
}
精彩评论