Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. select text box lines and remove

select text box lines and remove

Scheduled Pinned Locked Moved WPF
question
3 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    geomeo123
    wrote on last edited by
    #1

    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?

    R 1 Reply Last reply
    0
    • G geomeo123

      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?

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • R Richard Deeming

        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

        G Offline
        G Offline
        geomeo123
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups