• 1.6k
  • 5
  • 11.5k

draw a rectangle around a control that is getting hidden by another control

Apr 7 2008 5:43 PM

I have a panel that I'm trying to draw a blue border around. I'm using the following code to draw the blue rectangle. The problem is when I add a control to the panel and sets its dockstate to fill it hides the blue border.


With my example I derrive the graphics object from the control. And as a result the control hides the blue rectangle because its underneath the control.


I don’t want to draw a rectangle on the panels container as the panel fills the container.

I don’t want to draw a rectangle on the control. For other reasons…


The graphics object is derived from object where the graphic is to be drawn.




  1. Is is possible to draw a graphic and control the Z order so that the graphic is drawn at the top and is visible?
  2. How can I draw the rectangle and make it visible?



Private Sub DrawRectanle



        Dim myGraphics As Graphics

        Dim myRectangle As Rectangle

        Dim myPen As New Pen(Color.Red)


        'return the current form as a drawing surface

        myGraphics = Panel1.CreateGraphics()


        myRectangle = New Rectangle(Panel1.Location.X, Panel1.Location.Y, Panel1.Size.Width, Panel1.Size.Height)


        'draw rectangle from pen and rectangle objects

        myGraphics.DrawRectangle(pen:=myPen, rect:=myRectangle)


    End Sub

Answers (4)