Johannes Schacht
Johannes Schacht

Reputation: 1334

Show right end of text line in TextBox

I have TextBox for entering a file name. With a long path the TextBox can only show a part of the path. I consider the rightmost part (containing the file name) more relevant than the left part. However the TextBox displays only the leftmost part. I thought HorizontalContentAlignment would do the trick but it doesn't. What can I do?

Upvotes: 5

Views: 3417

Answers (1)

Gary Wright
Gary Wright

Reputation: 2469

As mentioned in the comments, the TextBox will automatically do what you want as you start typing into it, but I'm guessing that you also want it to do this if you set the Text from the designer (or programmatically).

Consider the following markup:

<TextBox Width="50" Height="30" Name="MyTextBox">This is some text</TextBox>

When executed, this will show the left portion of the text. To show the right portion, you could do something like this in your code behind:

public MainWindow()
{
    InitializeComponent();

    // The text box needs to have the focus for Select to work
    MyTextBox.Focus();
    // Move the caret to the end of the text box
    MyTextBox.Select(MyTextBox.Text.Length, 0);            
}

This example shows doing this in the constructor for the window, but you can do it wherever makes sense in your application.

Upvotes: 4

Related Questions