Enable/Disable TextBox Scrolling?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kfank
    New Member
    • Jan 2010
    • 1

    Enable/Disable TextBox Scrolling?

    I have a multi-line TextBox with a vertical scrollbar that logs data from a realtime process. Currently, whenever a new line is added [AppendText()] the textbox scrolls to the bottom so you can see the last entry. I would like to suspend this behavior if the user has used the scrollbar to view somewhere else in the textbox. Is there a preferred way to do this? There does not appear to be any mouse events for the scroll bar - they only fire over the client area of the TextBox.
  • RobyV
    New Member
    • Feb 2010
    • 4

    #2
    What about changing the TextBox with a RichTextBox? AFAIK with the RichTextBox you can handle the ScrollToCaret which enables the RichTextBox to scroll to the bottom. Then if you fire a function on the MouseOver event of the RichTextBox you can enable or disable the scroll as you wish.

    Hope it fixes.

    Comment

    • alexis4
      New Member
      • Dec 2009
      • 113

      #3
      It works with a textbox also, but refresh doesn't seem smooth, so a rich textbox is actually better.
      Code:
      if (textBox1.TextLength > 0)
      {
      textBox1.Select(textBox1.TextLength - 1, textBox1.TextLength);
      textBox1.ScrollToCaret();
      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        AppendText always scrolls.
        if you can check to see if the user has scrolled elsewhere, then use textBox1.Text+= StringText;
        That will add text without scrolling.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Why does everyone always go for the textbox for this when they start out?

          I would suggest replacing the multiline textbox with a listview.
          • You can add to its .Items collection to put new entries in the list.
          • You have more control over scrolling etc.
          • When it starts filing up with hundreds of lines you can just .Remove() from the top of the .Items etc.
          • You can have nice columns with headers.
          • You can have separator lines.


          You can see one used in my Diagnositcs pallet that I use in all my applications
          [IMGNOTHUMB]http://files.me.com/tlhintoq/t4nvy4[/IMGNOTHUMB]

          Take a little time to make it nice. In this example if the "[x] Scroll to newest" is checked then the listbox rolls each time a new item is listed to keep you up to date. If it's not checked (like when you are look back in time) then it doesn't.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Does listview support individual character selection and copy/pasting?

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Does listview support individual character selection and copy/pasting?
              Never tried it, but I doubt it. ListView works with items, so each entry is a basically a line item. You can multi-select several items. I have done drag-n-drop from listbox to xxxx which is a copy of the text. But I don't think it natively support selection of individual substrings. Of course you could easily write something that grabs an item, then selects substring(5,15) and copies it whenever the user clicks the "Copy event" button: Ensuring its always the same data.

              If you get just a little creative about it: The ListView items are objects not just text as in a textbox. You can send a LOT more than just text. You can send an entire object whose override ToString() places what you want to read in the ListView, but can contain just about anything else you might need like a bitmap screenshot. Now you can save both the date/time/condition and a screenshot of an issue all programmaticall y.

              I usually use it in the context provided by the OP, as a window into the log file.

              For a log I raise an event of
              Code:
              LogThis(this, "Started");
              Then two subscribers hear that event. One puts the entry in the diagnostic pallet, one writes it in the log text file. Each handler can then apply their own formatting.

              The pallet entries would then become:
              Code:
              09:24:42.021 - MainForm: Started
              09:24:42.103 - SignIn: Successful
              Where you have the time, the sending object (provided by 'this' in the event) and the string sent.

              The logger class breaks it into different log files based on the date, time and sending class, plus a summary log file. This way I can quickly look at the history of one thing (like a setup dialog, or the signin dialog), as well as get an over view of the entire application. New files are automatically generated by the hour so: 0900, 1000, 1100 etc. makes it easy to quickly find information about something reported at 1310hrs. And they can be archived/deleted by the day/month/year as needed for space.

              Code:
              C:\\{AppName}\{year}\{month}\{date}\MainForm_0900hrs.txt
              09:24:42.021 - Started
              
              C:\\{AppName}\{year}\{month}\{date}\SignIn_0900hrs.txt
              09:24:42:103 - Successful - Username = xxxxx
              
              C:\\{AppName}\{year}\{month}\{date}\CompositeLog_0900hrs.txt
              09:24:42.021 - MainForm: Started
              09:24:42.103 - SignIn: Successful

              But it could just as easily be a cash register receipt: Send it a 'product'. ToString() the SKU, description and price. Log the SKU and clerk ID number for the audit trail

              Comment

              Working...