Link to home
Start Free TrialLog in
Avatar of Krash_io
Krash_io

asked on

Textbox with border color in vb .net

Hi, I've been trying to build a textbox with a border color property in VB .Net. I'm not having any luck.  For some reason the OnPaint method never executes.  If i inherit from any control other than a textbox, it does execute!  Can someone please tell me what's wrong with this code?

---------------------------------------------------------------------------------------------------
Public Class myTextbox
    Inherits System.Windows.Forms.TextBox

    Private m_BorderColor As System.Drawing.Color = System.Drawing.Color.Red

    Public Function GetBorderColor() As System.Drawing.Color
        Return Me.m_BorderColor
    End Function
    Public Function SetBorderColor(ByVal clrBorder As System.Drawing.Color) As Boolean
        Me.m_BorderColor = clrBorder
        Me.Invalidate()
    End Function

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim penBorder As New Pen(Me.GetBorderColor, 1)
        Dim rectBorder As New Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1)
        e.Graphics.DrawRectangle(penBorder, rectBorder)
    End Sub

End Class
---------------------------------------------------------------------------------------------------

Thank you!

Krash_IO
Avatar of YZlat
YZlat
Flag of United States of America image

Avatar of Krash_io
Krash_io

ASKER

I saw that post before, which is where I got the code to draw the border.  I don't think I was able to get that code to work either.  Also, I'm trying to do this by subclassing a regular textbox, adding a border color property, and overridding the OnPaint method.  

Do you know why the code I have doesn't work?


Thanks
ASKER CERTIFIED SOLUTION
Avatar of KarunSK
KarunSK
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!!  I was so frustrated with this problem!  So, the drawing of the text box was being handled by the operating system, instead of by my code?  Which is why I could never see the OnPaint method fire?  

I guess some controls aren’t as tied to the operating system, because if I inherited from other controls, like a combo box, the OnPaint method would fire without explicitly setting the UserPaint bit in MyBase.SetStyle.

The code above works by just adding what you said, but I’m noticing some drawing errors with the text.  I’ll post the code when I feel it’s done and hopefully save someone else a headache!


Thanks again!

Krash_IO
You got the reason spot on. But why a Label, which is also derived from Control, has its OnPaint fired but TextBox does not is beyond me. "All controls are derived equal, but some are more derived than others?" ;-)

And yes, the caveat to overriding OnPaint is that they may be some unwanted behavior since you are handling the Paint. The worst case is that you may have to override WndProc and handle the WM_PAINT message yourself.

Good luck.
Karun.