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. C#
  4. TextBox: erasing the old lines

TextBox: erasing the old lines

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 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
    goldoche
    wrote on last edited by
    #1

    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!!!

    J 1 Reply Last reply
    0
    • G goldoche

      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!!!

      J Offline
      J Offline
      Jeff Varszegi
      wrote on last edited by
      #2

      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;
          }

      L 1 Reply Last reply
      0
      • J Jeff Varszegi

        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;
            }

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Jeff Varszegi wrote: Warning: poorly-designed code I beg to differ :) This is most likely the best way to make large changes to lines.

        top secret AdvancedTextBox

        J 1 Reply Last reply
        0
        • L leppie

          Jeff Varszegi wrote: Warning: poorly-designed code I beg to differ :) This is most likely the best way to make large changes to lines.

          top secret AdvancedTextBox

          J Offline
          J Offline
          Jeff Varszegi
          wrote on last edited by
          #4

          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.

          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