How to save a font in an inherited text box?

I use the following code to get a TextBox that does not draw 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 be missing something inside OnPaint () because my Font is no longer the default font for the text field (maybe I need to override another event).

When checking the CustomTextBox.Font property, it shows me, by default, “Microsoft SansSerif at 8.25”, but when entering text into my text block, the font definitely looks bigger and bold.

I hope you help me!

Hello,

Inno

[EDIT]

, OnPaint, CustomTextBox . OnPaint ( ). , - , OnPaint ( ATM , ).

+3
4

... ... , --- PERIOD.

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

, . , , . , , , ( ) [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

+2

TextBox, , , TextBox , .

+2

SetStyle, , " " :

if (IsHandleCreated)
{
     SetStyle(ControlStyles.UserPaint, true);
}
+2

Using SetStylein the text box will always ruin the picture according to this answer .

However ... is there a reason you can't just set it BorderStyleto None?

If you need, you can even change BorderStyleso that the default value is None, for example:

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MyControls
{
  // Apply ToolboxBitmap attribute here
  public class CustomTextBox : TextBox
  {
    public CustomTextBox()
    {
      BorderStyle = BorderStyle.None;
    }

    [DefaultValue(typeof(System.Windows.Forms.BorderStyle),"None")]
    public new BorderStyle BorderStyle
    {
      get { return base.BorderStyle; }
      set { base.BorderStyle = value; }
    }
  }
}
0
source

Source: https://habr.com/ru/post/1714214/


All Articles