Click here to Skip to main content
12,349,778 members (27,113 online)
Click here to Skip to main content
Add your own
alternative version

Stats

40.8K views
786 downloads
29 bookmarked
Posted

Show All TextBox Text After Resize

, 16 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
If a TextBox is wide enough to show all text, this method ensures it is visible after a resize.

Introduction

If you have a TextBox that is too narrow to display all its text, it will show the end of the text on startup:

BeforeResize.jpg

If you resize it to a width that could display the whole string, the TextBox will not reposition the text to show all text:

UnmodifiedResize.JPG

This code will reset the text position to show all text, while maintaining any text selection and carat position.

ModifiedResize.JPG

Background

The .NET TextBox control is merely a wrapper for the old Win32 edit box control. This limits its functionality, and makes it difficult to customize. Even simple tasks like resizing the height of the TextBox require you to use complex workarounds to accomplish.

When you resize a TextBox where some of the text is off the left edge of the control, it never repositions the text to show as much as the control will handle. To overcome this limitation, we need to modify the TextBox text selection.

Using the code

To reset the visible text in the TextBox, we need to change the carat (cursor) position to the beginning of the string, then back to the end. However, doing this will lose any selection that the user might have made prior to the resize. This routine will determine what is selected in the control, reset the carat location, then restore the user's selection.

Visual Basic:

Private Sub SetAllTextVisible(ByRef tbTextBox As TextBox)
    Dim iSelectStart As Integer
    Dim iSelectLength As Integer

    ' Get the current selection so we can re-select it.
    iSelectStart = tbTextBox.SelectionStart
    iSelectLength = tbTextBox.SelectionLength

    ' De-select everything and set the cursor to
    ' the start of the line
    tbTextBox.Select(0, 0)
    tbTextBox.SelectionStart = 0

    ' Restore the user's original selection
    tbTextBox.Select(iSelectStart, iSelectLength)
End Sub

C#:

public void SetAllTextVisible(TextBox tbTextBox)
{
    int startselect;
    int selectlength;

    // Get the current selection so we can re-select it
    startselect = tbTextBox.SelectionStart;
    selectlength = tbTextBox.SelectionLength;
    // De-select everything and set the cursor to the
    // start of the line.
    tbTextBox.Select(0, 0);
    tbTextBox.SelectionStart = 0;

    // Restore the user's original selection
    tbTextBox.Select(startselect, selectlength);
}

User Control

Alternatively, you could create a new UserControl class and change the inheritance on the class declaration from UserControl to TextBox. Override the OnSizeChanged event to call the SetAllTextVisible routine.

Important: If you change a class inheritence from UserControl to TextBox, you will need to open the UserControll.Designer code and remove the Autoscale property:

private void InitializeComponent()
{
    components = new System.ComponentModel.Container();
    //Comment the line below when changing inheritence from UserControl
    //to Textbox.
    //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}

public partial class ShowAllTextBox : TextBox
{
    public ShowAllTextBox()
    {
        InitializeComponent();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        SetAllTextVisible();
        base.OnSizeChanged(e);
    }

    public void SetAllTextVisible()
    {
        int startselect;
        int selectlength;

        // Get the current selection so we can re-select it
        startselect = this.SelectionStart;
        selectlength = this.SelectionLength;
        // De-select everything and set the cursor to the
        // start of the line.
        this.Select(0, 0);
        this.SelectionStart = 0;

        // Restore the user's original selection
        this.Select(startselect, selectlength);
    }
}

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hayes.adrian
Systems Engineer ThomsonReuters Tax & Accounting
United States United States
I am a Senior System Administrator for a 400+ server ASP farm. With such a large farm and limited staff, our goal is to add as much automation as possible to the system. Most of my programming consists of intelligent slack: spending 2 hours to write a program that handles a reoccurring 10 minute manual job.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Eswa8-Nov-10 0:33
memberEswa8-Nov-10 0:33 
GeneralPerfect. Thanks. Pin
mldisibio30-Sep-08 8:54
membermldisibio30-Sep-08 8:54 
GeneralExcellent and elegantly simple solution Pin
dgroves22-Sep-08 13:02
memberdgroves22-Sep-08 13:02 
GeneralYour Profile is interesting Pin
Dileep.M16-Sep-08 21:21
memberDileep.M16-Sep-08 21:21 
RantRe: Your Profile is interesting Pin
sk8er_boy28717-Sep-08 21:42
membersk8er_boy28717-Sep-08 21:42 
GeneralAnother way... Pin
sk8er_boy28716-Sep-08 20:14
membersk8er_boy28716-Sep-08 20:14 
GeneralRe: Another way... Pin
psouza4micronet17-Sep-08 3:35
memberpsouza4micronet17-Sep-08 3:35 
RantRe: Another way... [modified] Pin
sk8er_boy28717-Sep-08 21:38
membersk8er_boy28717-Sep-08 21:38 
GeneralWhy I voted "5" Pin
Humble Programmer16-Sep-08 11:59
memberHumble Programmer16-Sep-08 11:59 
GeneralRe: Why I voted "5" Pin
Ilíon16-Sep-08 15:23
memberIlíon16-Sep-08 15:23 
Joke[Message Deleted] Pin
sk8er_boy28716-Sep-08 20:09
membersk8er_boy28716-Sep-08 20:09 
GeneralRe: Why I voted "5" Pin
Johnny J.16-Sep-08 22:12
memberJohnny J.16-Sep-08 22:12 
GeneralRe: Why I voted "5" Pin
Ilíon17-Sep-08 5:50
memberIlíon17-Sep-08 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 16 Sep 2008
Article Copyright 2008 by hayes.adrian
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid