TextBox: erasing the old lines
-
Is there an easy way to delete the (old) last lines of a multiline TextBox? Cos now I have to set the MaxLength to a huge number... Thanks!!!
I'm still not all that familiar with Windows Forms, although I've studied it. Can't you do something like this? (Warning: poorly-designed code)
public static void RemoveTopLines(TextBox textBox, int linesToRemove) {
if (textBox == null) {
throw new ArgumentNullException();
}
else if (linesToRemove < 0) {
throw new ArgumentException("Invalid line count (" + linesToRemove + ")");
}
string[] lines = textBox.Lines;
int currentLineCount = lines.Length;
int newLineCount = lines.Length - linesToRemove;
if (newLineCount < 0) {
newLineCount = 0;
}
string[] newLines = new string[newLineCount];
if (newLineCount > 0) {
Array.Copy(lines, linesToRemove, newLines, 0, newLineCount);
}
textBox.Lines = newLines;
} -
I'm still not all that familiar with Windows Forms, although I've studied it. Can't you do something like this? (Warning: poorly-designed code)
public static void RemoveTopLines(TextBox textBox, int linesToRemove) {
if (textBox == null) {
throw new ArgumentNullException();
}
else if (linesToRemove < 0) {
throw new ArgumentException("Invalid line count (" + linesToRemove + ")");
}
string[] lines = textBox.Lines;
int currentLineCount = lines.Length;
int newLineCount = lines.Length - linesToRemove;
if (newLineCount < 0) {
newLineCount = 0;
}
string[] newLines = new string[newLineCount];
if (newLineCount > 0) {
Array.Copy(lines, linesToRemove, newLines, 0, newLineCount);
}
textBox.Lines = newLines;
}Jeff Varszegi wrote: Warning: poorly-designed code I beg to differ :) This is most likely the best way to make large changes to lines.
-
Jeff Varszegi wrote: Warning: poorly-designed code I beg to differ :) This is most likely the best way to make large changes to lines.
Thanks! I mostly mean that I didn't take the time to think about synchronization issues, and I didn't spend the normal half hour agonizing over every parameter name, etc. :) I also didn't test whether it'd be faster just getting the text as a string, and calling IndexOf repeatedly to skip past the indicated number of new-lines in the string; that might be faster, but I didn't have the time.