0

I tried to change the look of WinForms custom TextBox by overriding OnPaint or/and WndProc method. But in both cases before my method is invoked Windows already draws basic TextBox. What's draw here

And what my method does, it's drawing on top of it.

So do you know any solution to this problem ?

Documentation says that it could be unachievable (link below:) https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/constituent-controls

public class TestTextBox : TextBox
{
    public TestTextBox() : base ()
    {
        SetStyle(ControlStyles.UserPaint, true);
        InitializeComponents();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // base.OnPaint(e);
        e.Graphics.DrawLine(new Pen(Color.Blue),10,10,70,10);
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // base.OnPaintBackground(pevent);
        pevent.Graphics.DrawLine(new Pen(Color.Red),10,10,90,10);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PAINT)
        {
            Graphics g = this.CreateGraphics();
            g.DrawLine(new Pen(Color.BlueViolet), 10, 10, 70, 10);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    private void InitializeComponents()
    {
        this.BackColor = Color.Black;
        this.ForeColor = Color.White;
    }
}
Grzeszny
  • 13
  • 1
  • 6
  • 1
    If you want the functionality of a textbox and a custom drawn control, I'd recommend creating a CustomControl with an invisible textbox. Use the textbox internally for whatever you want to do and have the [CustomControl](https://learn.microsoft.com/de-de/dotnet/framework/winforms/controls/developing-custom-windows-forms-controls) do the painting. – Fildor May 06 '19 at 11:34
  • The customization is limited, take a look at [TextBox BorderColor](https://stackoverflow.com/a/39420512/3110834) for example. – Reza Aghaei May 06 '19 at 14:37
  • @Fildor what about for example: SelectionStart, Multiline, CharacterCasing properties ? I think just invoking the textbox internally won't do the job :/ – Grzeszny May 06 '19 at 16:26
  • Well, that's what you got. Take the standard, roll your own _or_ go and find some Control-Collection that meets your requirements (something like DevExpress, ... but some of them are not free). – Fildor May 07 '19 at 06:43
  • @Fildor the main problem is that my CadCustomTextBox implement a plenty of overrider behavior e.g. when we have more text, and I trying to figure out, the easiest way to change it's look, without loosing the functionality or redeploying it on created screens :/ – Grzeszny May 07 '19 at 09:23

0 Answers0