Show right end of text line in TextBox

Johannes Schacht

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?

Gary Wright

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Related Related

HotTag

Archive