Search code examples
winformstextboxalignment

Winforms TextBox vertical alignment with FixedSingle border


When you change the BorderStyle property of a TextBox control to FixedSingle, the entered text is no longer properly vertically centered inside of the control. The padding on the bottom is much larger than on the top. How can I get rid of this?

This is on Windows 7.

Textbox


Solution

  • Yeah, it's a bit of a turd. One way to cheat is to change the border to none and paint the border around the borderless textbox in the container's paint event:

    Rectangle r = new Rectangle(textBox1.Left - 3, textBox1.Top - 3,
                                textBox1.Width + 5, textBox1.Height + 5);
    e.Graphics.FillRectangle(SystemBrushes.Window, r);
    e.Graphics.DrawRectangle(SystemPens.WindowFrame, r);
    

    Results:

    enter image description here