0

I have a Control that ultimately inherits from TextBox

If I don't remove the border then the text vertical alignment is fine:

Aligned with border

But I want to remove the border like this:

public partial class MyReadonlyDataField : TextBox
{
  private void InitializeComponent()
  {      
    this.BorderStyle = System.Windows.Forms.BorderStyle.None;      
    this.Appearance.BorderAlpha = Infragistics.Win.Alpha.Transparent;
    this.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    this.Height = 19;
  }
}

But then the text alignment is off. How can I fix that?

Missaligned without border

juergen d
  • 201,996
  • 37
  • 293
  • 362

1 Answers1

0

I solved it. If I set the perfect height for my TextBox control then the text will be centered, because the control has the exact height that matches the text font.

To achieve that I set the hidden AutoSize property of TextBox. It can't be set in the Forms-Designer nor does IntelliSense list it as property. But it exists and works when set to true!

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • You can set the `Margin` property of Controls to compensate: defines the external distance (from the Container) – Jimi Aug 25 '22 at 10:57
  • But I would have to set that individually every time depending on the height of the control. I find it a bit better to let the control take care of that automatically. – juergen d Aug 25 '22 at 11:04
  • You said you have a Custom Control there. So, when you set the BorderSyle to none, you can automatically compensate. I cannot say whether the Infragistic's TextBox also derives from TextBoxBase, but the AutoSize feature of the TextBox changes its Height, which may have other consequences. Anyway, of course you do whatever you're more comfortable with. – Jimi Aug 25 '22 at 11:41