Drawing a textbox/combo

is there a way to override drawing a textbox or combo
i have a MyTextBox control that inherits from textbox, i want it to draw normally if a boolean normal is set to true, or draw as a button/rectangle/frame (or sometrhing else) if the boolean normal is false.
I tried overriding paint and paintbackground, not calling the base onpaint etc.. to no avail. I keep getting the normal textbox draw.
something simple like that:

Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)

   if not normal then
       ControlPaint.DrawGrid(pe.Graphics, ClientRectangle, new Size(5, 5), Color.Black)
   else
      MyBase.OnPaint(pe)
  end if

End Sub



Answer this question

Drawing a textbox/combo

  • Joer1970

    err what do you mean exactly
    i tried most combinations and none work,
    i tried SetStyle(Windows.Forms.ControlStyles.UserPaint, True) to do painting in the paint event,
    i tried overrding the paint event, paint background and wndproc to no avail.. i don't think anything else is left to try, does that simply mean it cannot be done if i inherit from a textbox control

  • michaelplevy

    There are some controls that, due to their implementation, do not respond to painting requests the same.  Textbox and ComboBox are two of these controls.
  • theRevenge

    That's what I'm saying.  The documentation says this:

    "Some Windows Forms controls, such as TextBox, are painted directly
    by Windows. In these instances, the OnPaint method is never called, and thus
    the above example will never be called."

    However, this works for me:


    Public Class tt
        Inherits TextBox

        Public Sub New()
            MyBase.New()
            Me.SetStyle(ControlStyles.UserPaint, True)
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Blue, ButtonBorderStyle.Solid)
            MyBase.OnPaint(e)
        End Sub
    End Class


  • Drawing a textbox/combo