1

According to my understanding, the code below should right-justify the text if the text is longer than the textbox can show, otherwise it keeps it left-justified.

The problem is that it doesn't actually do this and it's acting really odd. Short strings end up right-justified sometimes and long strings are always left-justified.

What am I doing wrong?

private void textBoxCurrentConfig_TextChanged(object sender, EventArgs e)
{
    SizeF stringSize = new SizeF();
    stringSize = TextRenderer.MeasureText(textBoxCurrentConfig.Text, textBoxCurrentConfig.Font);

    float currentTextWidth = stringSize.Width;
    float allowedTextWidth = textBoxCurrentConfig.Size.Width - 10;

    if (currentTextWidth >= allowedTextWidth) // if the text we want to display is larger than the textbox can hold, right justify it to show the filename
    {
        textBoxCurrentConfig.TextAlign = HorizontalAlignment.Right; // right justify                
    }
    else // otherwise we can display the entire path
    {
        textBoxCurrentConfig.TextAlign = HorizontalAlignment.Left; // left justify
    }

    textBoxCurrentConfig.Refresh();
    this.Refresh();
}
Kashif
  • 865
  • 2
  • 12
  • 33
  • what numbers are you getting for `current...` and `allowed...`? Does it work when you debug it? – Jodrell Apr 10 '12 at 09:07
  • I tested the code by stepping through it. The numbers make sense and the logic works out just fine. It's the alignment that's throwing me off here. – Kashif Apr 10 '12 at 09:12
  • @Kashif Are you using WinForms? From the code it looks that you set the cursor position according to the text lenght. If that's the case then you can use textBoxCurrentConfig.Select() method. – ABH Apr 10 '12 at 09:22
  • Yeah, I'm using WinForms. I've tried 'textBoxCurrentConfig.Select()' as you suggested but it doesn't seem to run the cursor position to either side. Perhaps there's an extra step after selecting the control? – Kashif Apr 10 '12 at 09:25
  • 1
    @Kashif It's simple, use textBoxCurrentConfig.Select(0, 0); and textBoxCurrentConfig.Select(textBoxCurrentConfig.Text.Length, 0); to move the cursor at the start or at the end. For details, check MSDN http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select.aspx – ABH Apr 10 '12 at 09:45
  • @hamad, this worked! Please post your response as an answer so I can check select it as the proper solution. Thanks much. – Kashif Apr 10 '12 at 16:42
  • @Kashif I have posted it as an answer. Thanks a lot for mentioning it, I appreciate :) – ABH Apr 10 '12 at 17:55

2 Answers2

3

As from your comments, you want to move cursor position according to the text length. You can use TextBox.Select() method for this. Check MSDN for details.

So if you want to move cursor at the the start of text, you can use

textBoxCurrentConfig.Select(0, 0);

and if you want to move cursor at the end of text, you can use

textBoxCurrentConfig.Select(textBoxCurrentConfig.Text.Length, 0);

ABH
  • 3,391
  • 23
  • 26
0

Try to remove

this.Refresh();

It's may cause the page to refresh and return the text box to the original align

David
  • 414
  • 1
  • 6
  • 16
  • I tried all combinations of this.Refresh(); and textBoxCurrentConfig.Refresh(); with no luck =/ – Kashif Apr 10 '12 at 09:10
  • You'll have to explain a bit more. I'm afraid I'm not knowledgeable about css – Kashif Apr 10 '12 at 09:13
  • it's pretty hard to explain it in a line, but I try: it's define the design of the page. so you might have a definition there that define the text to the right and override your style design – David Apr 10 '12 at 09:18
  • I'll look around and see if I can make some sense of the css. Thanks – Kashif Apr 10 '12 at 09:19
  • 1
    check this links, it may help:[text-align](http://www.w3schools.com/cssref/pr_text_text-align.asp), [direction](http://www.w3schools.com/cssref/pr_text_direction.asp) – David Apr 10 '12 at 09:25