2

I have several Textboxes in my Form which display filepaths. Mostly the filepaths are too long to display in the textbox. Is there a possibility to cut the surplus text and append some point characters to it and then align it right?

For example:
If the path is C:\Programs\anotherfolder\blabla\thisisatest.xml

A Textbox should show: ...lder\blabla\thisisatest.xml

If I resize the textbox, the text in it shall resize/expand with it.
Is there a way to do this automatically, maybe via Resize event of a textbox.

Thank you very much.

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
user3443859
  • 31
  • 1
  • 4

3 Answers3

0

This might help if you are looking for resizing your textbox based on the text entered. You can call this code on any event like when you move focus out of textbox or after the data is loaded in the textbox. You need to replace your textbox's actual text in below line in the code.

mySize = e.Graphics.MeasureString("This is a test", myFont);
Anil Soman
  • 2,443
  • 7
  • 40
  • 64
0

if this is a webpage ..

include 2 hidden variables as following

<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:HiddenField ID="HiddenField1" runat="server" />
    <asp:HiddenField ID="HiddenField2" runat="server" />
</div>

include jquery and the script as following

    <script type="text/javascript">
    $(document).ready(function () {
        $('#TextBox1').focus(function () {
            $('#TextBox1').val($('#HiddenField1').val());
        });

        $('#TextBox1').blur(function () {
            $('#TextBox1').val($('#HiddenField2').val());
        });
    });

</script>

set the fields in the code behind

    string str = @"C:\Programs\anotherfolder\blabla\thisisatest.xml";
    HiddenField1.Value = str;
    if (str.Length > 10)
    {
        TextBox1.Text = "..." + str.Substring(str.Length - 10, 10);
        HiddenField2.Value = TextBox1.Text;
    }

This will show the whole string only when focused on the textbox.

Here is a demo of how it will look like.

Let me know if this was helpful or if you have any queries

A J
  • 2,112
  • 15
  • 24
0

Use this code to remove surplus text:

private string GetShortText(string longText)
{
    int validTextSize = 27;
    if (longText.Length <= validTextSize)
        return longText;
    return "..."+longText.Substring(longText.Length - validTextSize);
}

And use above code like this:

string longText = @"C:\Programs\anotherfolder\blabla\thisisatest.xml";
txtPath.Text=GetShortText(longText);

An for change alignment, if your form is a Windows Form you can use this code:

txtPath.TextAlign = HorizontalAlignment.Right;

Or if your form is a WPF form use this code:

txtPath.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
Merta
  • 965
  • 1
  • 11
  • 23