views:

154

answers:

2

Hi SO-lers,

I'm using the following code to get a TextBox that is not drawing its borders:

public partial class CustomTextBox : TextBox
{
 public CustomTextBox()
 {
  InitializeComponent();
  SetStyle(ControlStyles.UserPaint, true);
 }

 protected override void OnPaint(PaintEventArgs e)
 {
  base.OnPaint(e);

  int borderWidth = 1;

  ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle,
      Color.Transparent, borderWidth, ButtonBorderStyle.Solid,
      Color.Transparent, borderWidth, ButtonBorderStyle.Solid,
      Color.Transparent, borderWidth, ButtonBorderStyle.Solid,
      Color.Transparent, borderWidth, ButtonBorderStyle.Solid);
 }
}

I seem to miss something inside of OnPaint() because my Font is not the default Font for a textBox anymore (perhaps I have to override another event).

When checking CustomTextBox.Font property it shows me the default "Microsoft SansSerif in 8,25" but when typing text into my textBox the Font definitely looks bigger and bold.

Hope you can help me!

Regards,

inno

[EDIT]

I should mention that if I don't override OnPaint the Font of my CustomTextBox is correct. Only when overriding OnPaint my Font is incorrect (when typing text the font is bigger and seems to be bold). So I think I have to do something to initialize the font correctly inside of OnPaint (but ATM I have no idea how to do this).

+1  A: 

Two options for you to look at... In my base-class, I'm forcing a read-only font definition... similar for other controls, so other developers with the class can't change it --- PERIOD.

[ReadOnly(true)] public override Font Font { get { return new Font("Courier New", 12F, FontStyle.Regular, GraphicsUnit.Point); } }

Second option, and I did not actually use is during the serialization of the form. I can't take credit, nor remember where I found elsewhere in this forum, but may help too. Apparenty, by hidding the serialization visibility, it doesn't FORCE each individual control's property (in this case, apply for your font) [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

HTH

DRapp
Added both approaches into my CustomTextBox class without success. thanks, anyway.
Inno
+1  A: 

If you don't explicitely set the font of a TextBox, it gets its font from its parent and ancestors, so if the TextBox is on a Panel, it gets its font from that Panel, or from the parent Form.

Tommy Carlier
Parent has same Font "Microsoft SansSerif 8,25" but when typing I don't think it's this Font.
Inno