Solved

Textbox with border color in vb .net

Posted on 2004-03-23
5
4,547 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

713 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