VB.NET - paint a custom border around a control

I want to paint a custom border around a control. The border needs to resize appropriately.
tricks801Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
Something like this?
 Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        g.DrawRectangle(Pens.Blue, New Rectangle(CheckBox1.Left - 3, CheckBox1.Top - 3, CheckBox1.Width + 6, CheckBox1.Height + 6))
    End Sub

Open in new window

0
tricks801Author Commented:
will that work even when the form is resized. The control in my case is a grid and its anchored to all sides.
0
PaulHewsCommented:
It will work if you invalidate the form in the resize event so that it's repainted properly.  (Full sample, just add a checkbox to a form and the following code)
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CheckBox1.AutoSize = False
        CheckBox1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        g.DrawRectangle(Pens.Blue, New Rectangle(CheckBox1.Left - 3, CheckBox1.Top - 3, CheckBox1.Width + 6, CheckBox1.Height + 6))
    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Invalidate()
    End Sub
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.