Solved

.Net: How get a TextBox scroll bar to stay at bottom to build a scrolling status display?

Posted on 2008-10-14
15
1,342 Views
Last Modified: 2013-12-17
Background: I have a standard System.Windows.Forms.TextBox on a form and want it to scroll status messages for my program like this:

  event a
  event b
  event c
  ...

with the newest event always on the bottom.  The problem is this: The events list from top to bottom but the scroll bar stays at top and I have to manually scroll down to see new events as they are added.  I want this automatic.

Question: How do you get a TextBox scroll bar to stay at the bottom so the newest text added is always shown?

Details: I set:

MultiLine=True
ScrollBars=Vertical

and am appending text like this:

tbOutput.Text += "Some Event Message\r\n";

Challenge: Is TextBox the "right tool for the job" or is there a better component?
0
Comment
Question by:ZuZuPetals
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
15 Comments
 
LVL 7

Expert Comment

by:technofile
ID: 22715249
It seems like you are using a text box where you should be using a listbox. If you use a listbox you can set the view to the bottom any time you want by simply saying
ListBox1.SelectedItem = ListBox1.Items.Count - 1
0
 
LVL 7

Expert Comment

by:technofile
ID: 22715312
Oh and to add items to a listbox you just say

ListBox1.Items.Add("Some event information")
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 22715768
The easiest way to do this in a textbox is to select the last character of text and then scroll to the selection.

I wrote up a quick sample that checked to see if the vertical scroll bar was active and then scrolled to the bottom using the following code:

        private void status_TextChanged(object sender, EventArgs e)
        {
            if((status.ScrollBars & ScrollBars.Vertical) == ScrollBars.Vertical)
            {
                status.Select(status.Text.Length - 1, 1);
                status.ScrollToCaret();
            }
        }

I would continue to use a TextBox in this instance because you can select the text out of it for pasting into email and the like, especially if you're going to report any critial information that can be used for debugging the application at a later date - although if this is going to customers you may want to make it read-only so they can't change the messages on you  =)
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 6

Expert Comment

by:ebosscher
ID: 22715791
oh, it should be noted that the check in the previous message works with both ScrollBars.Vertical set as well as ScrollBars.Both (tested).  very nice of them to allow us to use bit checks.
0
 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22717600
Yours!

Using ListBox may be better

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 1; i++)
    {
        textBox1.Text += "Hello \r\n";
    }
 
    textBox1.Focus();
    textBox1.ScrollToCaret();
    textBox1.Select(textBox1.TextLength, textBox1.TextLength);
}

Open in new window

0
 
LVL 6

Expert Comment

by:ebosscher
ID: 22727079
I'm not following your post trunghieubkit, you say that a listbox may be better, but the code shows a textbox, and has no reasons for why a listbox may be better?

if you could elaborate i would really appreciate it  =)
0
 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22727603
I mean if u use ListBox, it can be easy to manage,
ex: u can select a row and create an event with it and so on,
In yours, I think that's enough,
0
 
LVL 7

Expert Comment

by:technofile
ID: 22735734
He was referring to my original post about list box's which are very simple to manage and are specifically designed to work with lists like you were trying to do. When you use a list box you can actually access each item you add by using listbox.items. You can get to the bottom by using listbox.selecteditem - listbox.items.count -1 and you add items by using listbox.items.add("text").
0
 
LVL 7

Expert Comment

by:technofile
ID: 22735740
correction listbox.selecteditem = listbox.items.count - 1

0
 
LVL 6

Expert Comment

by:ebosscher
ID: 22736957
The only issue i see with a list box is if you ever want to select information back out of the status message.  I suppose you could do a multi-select list box, i'm just curious what would happen if you tried to copy that to the clipboard?  Would you need to write code to handle the copy shortcut, or would it just know to take all of the elements you had selected and copy them to the clipboard?

When this type of mechanism is used for status messages you probably want to be able to easily copy those messages back out and send them to support staff if there is an issue with the software, and I don't know how easy that would be with a listbox - and no, I haven't coded up a little test to check it out =)
0
 
LVL 7

Accepted Solution

by:
technofile earned 500 total points
ID: 22744729
Well you would need to write up a little something to get the information out but that would also allow you to get any and all information you need and format it properly, or better yet send it directly to the supports staff. I definitely would say the little bit of extra work you have to do to get the information back out of a listbox won't even come close to the frustration you are going to feel with the inability to have structure and organization trying to deal with a textbox.

Well good luck with what you choose.
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 22947414
I would object.

I know my answer solves the immediate question, and attempts to address the larger one, and technofile's answers, although I don't agree with the larger question will work to solve the immediate issue as well.
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 22973786
LOL, not the way i saw that one going....
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question