Trying to make a list out of a string of part names. It's skipping displaying all the names except the very last one in the list.. Why is it doing this? Is there a better way of doing this?
My Code behind
List<string> Ta1PartNumbers = ta1input.Split('\n').ToList<string>(); //Trying to split at the line break.. Would this be the proper way to do it?
foreach (string Tpartnumber in TramPartNumbers)
{
Div1.InnerText = (Tpartnumber);
}
List is like so:
Part_numbers
1017foo
1121bar
etc..
You're resetting the innerText value each iteration of the loop, so it only retains the last one. Try appending with the += operator
foreach (string Tpartnumber in TransimPartNumbers)
{
Div1.InnerHtml += Tpartnumber + "<br />";
}
I'd avoid using a List where you don't need to. Use this split method instead, but I think you want \r\n... And like the other posters, you needed += which appends, rather than + which updates.
string[] lines = Regex.Split(ta1input, "\r\n");
foreach (string line in lines)
{
Div1.InnerText += Tpartnumber;
}
I make this mistake more than I should. Try:
Div1.InnerText = "";
foreach (string Tpartnumber in TransimPartNumbers)
{
Div1.InnerText += (Tpartnumber);
}
You are basically overwriting the previous value each time.
Each iteration of your loop replaces the InnerText of Div1 with the current string. This is why you are only getting the last string, you wrote over all the previous strings.
But, why even split the string? If you just want to write all of the strings to the div, you can just do:
Div1.InnerText = ta1Input;
What do you want as your final HTML?
Part_numbers<br/>
1017foo<br/>
1121bar<br/>
etc..<br/>
Or
<div>Part_numbers</div>
<div>1017foo</div>
<div>1121bar</div>
<div>etc..</div>
Or something else? Personally, I prefer the latter. I'd do something like this:
string[] Ta1PartNumbers = ta1input.Split('\n');
foreach (string Tpartnumber in Ta1PartNumbers)
{
var div = new HtmlGenericControl("div");
div.InnerText = Tpartnumber;
Div1.Controls.Add(div);
}
Related
Greets.
I'm calling a Window with .ShowDialog() and returning some lines form a textbox.
The lines return back to a List<>, but each character in the textbox getting returned is getting assigned to it's own index value within the List<>.
I essentially want to add an entire line from the textbox to it's own index value in the List<>
EXAMPLE:
I enter the below in the textbox that was called from the ShowDialog();
123456
87564
125
How do I add each line from the textbox to it's own index on the list?
This is what I have now. (No code on the textbox window that I enter these values into) (I realize I spelled it as imput...) When I debug and review the pos List<>, each character has it's own index ID..
private void GetPOs()
{
MultiLineImput getPOList = new MultiLineImput();
getPOList.ShowDialog();
foreach (char po in getPOList.listOfPOs.Text)
{
pos.Add(po.ToString());
}
if (pos.Count > 0)
{
string a = String.Join("", pos);
MessageBox.Show(a, "POs to Process");
}
else
{
if (!getPOList.wasCanceled.Equals(1))
{
MessageBox.Show("No values were passed", "Warning");
}
}
}
You're iterating over the characters of Text property, so each character is converted to string and added to list separately.
I'm not sure what you mean by adding the entire "line". In your example there's only one line, so you can rewrite this loop
foreach (char po in getPOList.listOfPOs.Text)
{
pos.Add(po.ToString());
}
to simply
pos.Add(getPOList.listOfPOs.Text);
if you meant to split this line in entries "123456", "87564", "125", you can do the following way:
foreach (string po in getPOList.listOfPOs.Text.Split(' '))
{
pos.Add(po);
}
if your textbox indeed support multiline input, you can split by Environment.NewLine, like this:
foreach (string po in getPOList.listOfPOs.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
{
pos.Add(po);
}
If you iterate over a string, the iterator will pull one character at a time. It has no idea what a line break is.
I suggest you break the string up by line breaks, then iterate over the result, like so:
MultiLineInput getPOList = new MultiLineInput();
getPOList.ShowDialog();
var wholeText = getPOList.listOfPOs.Text;
var lines = wholeText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string po in getPOList.listOfPOs.Text)
{
pos.Add(po);
}
//Etc.....
I have a list of strings stored in an ArrayList. I want to split them by every occurrence of ';'. The problem is, whenever I try to display them using MessageBox, there's an excess space or unnecessary value that gets displayed.
Sample input (variable = a):
Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;
Below is a line of code I used to split them:
string[] display_document = (a[0] + "").Split(';');
Code to display:
foreach (object doc_properties in display_document)
{
TextBox aa = new TextBox();
aa.Font = new Font(aa.Font.FontFamily, 9);
aa.Text = doc_properties.ToString();
aa.Location = new Point(pointX, pointY);
aa.Size = new System.Drawing.Size(80, 25);
aa.ReadOnly = true;
doc_panel.Controls.Add(aa);
doc_panel.Show();
pointY += 30;
}
The output that displays are the following:
How do I remove the last occurrence of that semicolon? I really need help fixing this. Thank you so much for all of your help.
Wouldnt It be easiest to check if the input ends with a ";" before splitting it, and if so remove the last character? Sample code:
string a = "Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
if (a.EndsWith(";"))
{
a = a.Remove(a.LastIndexOf(";"));
}
//Proceed with split
Split will not print last semicolon if no space character is added and your input is a string.
I don't know why you prefer an array list (which probably is the reason of this strange behaviour) but if you could use your input as a string you could try that
string a = "Arial;16pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
string[] display_document = a.Split(';');
foreach (object doc_properties in display_document)
{
//The rest of your code
}
I have this code:
System.IO.Directory.CreateDirectory(foldercreationPATH.Text + "\\Tosystem\\" + customersBOX.Text);
Now, if user specifies folder(names) to be created separated by comma, how can I do it?
E.g. if textbox contains "customer1, customer2", then I would like to create separate folders for these.
You would take the string inside the textbox and use the
.split()
method to create an array which you can loop through using a
foreach
loop. I also invoked the
.Trim()
method to remove any trailing white space.
string[] strArr = customersBOX.Text.Split(',');
foreach (string item in strArr)
{
item.Trim();
System.IO.Directory.CreateDirectory(foldercreationPATH.Text + "\\Tosystem\\" + item);
}
you can try this.
var dirs = customersBOX.Text.Split(",".ToArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (var dir in dirs)
{
var finalPath = Path.Combine(foldercreationPATH.Text, "Tosystem", dir);
Directory.CreateDirectory(finalPath);
}
This is a bit of an odd question, and I'm sure there was an easier way of doing this but...
I have a method that returns a List of names. I am parsing through this list with a foreach loop. I am adding each name to one long String so that I can set the text of a Table Cell to that string. But I can't seem to get it to add a new line. Is this possible?
Here's a snippet of the code:
Earlier my table loop:
TableCell tempCell = new TableCell();
The issue:
// Returns a List of Employees on the specified day and time.
List<string> EmployeeList = CheckForEmployees(currentDay, timeCount);
string PrintedList = "";
foreach (String s in EmployeeList)
{
PrintedList += s;
// PrintedList += s + System.Environment.NewLine;
// PrintedList += s + " \n";
}
tempCell.Text = PrintedList;
Both the commented code lines didn't work. Any ideas?
You need to append a break tag since you want the new line to show in HTML. So append <br/>. I would also recommend using a stringbuilder if it is more than a few iterations.
Table cell as in HTML table cell? Do you perhaps need to use the "br" tag instead of normal newline?
Another thing, you should use StringBuilder instead of doing string concatenation the way you do. Or even better, String.Join.
string PrintedList = String.Join("br-tag", EmployeeList);
Again, not sure if the br-tag is what you are after, but prefer to use the methods in the String class when possible.
I have a list with many line extracted from file and I want to display it a richTextbox with this code
foreach (string s in Dettaglio)
{
txtDettaglio.Text += s + Environment.NewLine;
}
And Dettaglio definition is:
System.Collections.Generic.List<string> Dettaglio = new System.Collections.Generic.List<string>();
But it makes a lot of time to accomplish it there’s any other solution or I haven’t to use richTextbox?
Firstly: I'd use AppendText instead of string concatenation:
foreach (string s in Dettaglio)
{
txtDettaglio.AppendText(s);
txtDettaglio.AppendText(Environment.NewLine);
}
It may be faster to use concatenation to avoid calling AppendText twice:
foreach (string s in Dettaglio)
{
txtDettaglio.AppendText(s + Environment.NewLine);
}
Now it could be that that won't actually be any faster, but it's what I'd try to start with - the internal data structure of RichTextBox may need to do work in order to fetch the Text property, and using AppendText you may avoid it having to reparse text that it's already handled.
Maybe using StringBuilder will be faster
StringBuilder sb = new StringBuilder();
foreach (string s in Dettaglio)
{
sb.Append(s + Environment.NewLine);
}
txtDettaglio.Text = sb.ToString();