Solved

Textbox with border color in vb .net

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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