Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Textbox with border color in vb .net

Posted on 2004-03-23
5
Medium Priority
?
4,566 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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