Solved

Textbox with border color in vb .net

Posted on 2004-03-23
5
4,531 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:Krash_io
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 10657479
0
 
LVL 1

Author Comment

by:Krash_io
ID: 10657520
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
0
 
LVL 6

Accepted Solution

by:
KarunSK earned 500 total points
ID: 10664556
Put this in the constructor of myTextBox:

SetStyle(ControlStyles.UserPaint, True)

Voila! You got a red border!

Karun.
0
 
LVL 1

Author Comment

by:Krash_io
ID: 10666257
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
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10671510
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now