tags:

views:

621

answers:

3

I'm concatenating a string that sometimes is long enough for it not to fit in a label control. How can i make it autoscroll to the rightside so i always see the end of the string?

+1  A: 

The TextAlign property allows you to specify the alignment. If you right-justify it with this, the right side of the text will always be visible. However, if you want it to be left or center justified and still have the behavior you describe, I suspect you will need to perform some measurement using Graphics.MeasureString to determine if the text fits and change the alignment dynamically.

Jeff Yates
A: 

AFAIK there's no way to scroll a label. A hack would be to use a TextBox (read-only, turn off border) then use SendKeys.Send() to move the cursor to the end of the text. Something like:

        textBox1.Focus();
        SendKeys.SendWait("{END}");

To get the text to not show up as selected I had to change it's position in the tab order (so that it wasn't 1) but that may not be a problem in your case.

Arnshea
+3  A: 

While I'm sure there are ways of doing, I have to ask, why? I think it would look and/or work very badly and probably confuse the user.

Why not have the text get trimmed with an ellipse (...) at the end and show a tooltip on the label?

using System.Windows.Forms;

var label = new Label();
label.AutoSize = false;
label.AutoEllipsis = true;
label.Text = "This text will be too long to display all together.";

var labelToolTip = new ToolTip();
labelToolTip.SetToolTip(label, label.Text);

Now the tooltip will show the full text when the user hovers over it. Since the text in the label will be truncated and end in an ellipse, the user should know to hover over for more info (usually the standard way).

Samuel