Okay so I'm trying to make a 'console' like text box within a form, however once you reach the bottom, instaid of being able to scroll up, it will just delete the top line, Im having some difficulties.
So far, when it gets to bottom it deletes the top line, however only once, it just carries on as normal. Here is my function:
StringBuilder sr = new StringBuilder();
public void writeLine(string input)
{
string firstline = "";
int numLines = Convert.ToString(sr).Split('\n').Length;
if (numLines > 15) //Max Lines
{
sr.Remove(0, Convert.ToString(sr).Split('\n').FirstOrDefault().Length);
}
sr.Append(input + "\r\n");
consoleTxtBox.Text = Convert.ToString(sr) + numLines;
}
Would be great if someone could fix this, thanks
Lucas
Best Answer
First, what's wrong with your solution: the reason it does not work is that it removes the content of the line, but it ignores the
\n
at the end. Adding1
should fix that:Now to doing it a simpler way: all you need to do is finding the first
\n
, and taking substring after it, like this:Note that this code does not crash even when there are no newline characters in the string, i.e. when
IndexOf
returns-1
(in which case nothing is removed).