select text box lines and remove
-
I have a textbox display that has incoming data from a serial port that I display. After 200 lines of data that has came in I want to remove 20 lines at the beginning. Here is my code:
private void TextBoxADCPDisplay_TextChanged(object sender, TextChangedEventArgs e)
{TextBoxADCPDisplay.SelectionStart = TextBoxADCPDisplay.Text.Length; TextBoxADCPDisplay.ScrollToEnd(); if (TextBoxADCPDisplay.LineCount > 200) { TextBoxADCPDisplay.Select(0, 20); TextBoxADCPDisplay.SelectedText = ""; }
}
I get some strange behavior. Where after I hit 200 lines I see multiple lines that are the same where they should be different, but if I manually scroll up a little and scroll back down the lines of data that are coming in display as they should. What am I doing wrong?
-
I have a textbox display that has incoming data from a serial port that I display. After 200 lines of data that has came in I want to remove 20 lines at the beginning. Here is my code:
private void TextBoxADCPDisplay_TextChanged(object sender, TextChangedEventArgs e)
{TextBoxADCPDisplay.SelectionStart = TextBoxADCPDisplay.Text.Length; TextBoxADCPDisplay.ScrollToEnd(); if (TextBoxADCPDisplay.LineCount > 200) { TextBoxADCPDisplay.Select(0, 20); TextBoxADCPDisplay.SelectedText = ""; }
}
I get some strange behavior. Where after I hit 200 lines I see multiple lines that are the same where they should be different, but if I manually scroll up a little and scroll back down the lines of data that are coming in display as they should. What am I doing wrong?
geomeo123 wrote:
TextBoxADCPDisplay.Select(0, 20);
TextBoxADCPDisplay.SelectedText = "";Well, start with the fact that the code you've written will not remove 20 lines; it will remove 20 characters.
TextBox.Select(Int32, Int32) Method (System.Windows.Controls) | Microsoft Learn[^]:
Parameters
start
The zero-based character index of the first character in the selection.length
The length of the selection, in characters.So unless you can guarantee that every line is precisely 1 character long, and that the
Select
method ignores the line-break characters, your code isn't going to do what you want it to do. If you want to remove 20 lines, you'll need to call TextBox.GetLineLength[^] for each line you want to remove, and sum up the line lengths to get the number of characters you need to select. NB: You'll need to test whether or not the returned value includes the line-break characters, as that's not clear from the documentation.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
geomeo123 wrote:
TextBoxADCPDisplay.Select(0, 20);
TextBoxADCPDisplay.SelectedText = "";Well, start with the fact that the code you've written will not remove 20 lines; it will remove 20 characters.
TextBox.Select(Int32, Int32) Method (System.Windows.Controls) | Microsoft Learn[^]:
Parameters
start
The zero-based character index of the first character in the selection.length
The length of the selection, in characters.So unless you can guarantee that every line is precisely 1 character long, and that the
Select
method ignores the line-break characters, your code isn't going to do what you want it to do. If you want to remove 20 lines, you'll need to call TextBox.GetLineLength[^] for each line you want to remove, and sum up the line lengths to get the number of characters you need to select. NB: You'll need to test whether or not the returned value includes the line-break characters, as that's not clear from the documentation.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I had this code for my old windows form. At the time I didn't pay much attention to it as I had found it on a forum somewhere and I just copied and pasted. The original problem was in Windows forms. After a certain period of time of taking data in the entire program would crash. The code above resolved it, but moving over to WPF I'm finding a lot of things don't work as they should or even have the same methods. I am liking the fact that WPF is a lot more forgiving on certain things though so all good. Thanks