Draw a transparent box in a VB2005 winforms application

O

\(O\)enone

To assist my users in identifying where a dragged control will be dropped on
my WinForms form, I wish to be able to draw a transparent box on the top of
my form which will surround the area into which the drop will take place.
Essentially I just need to be able to draw a rectangle on top of my form,
but the inside of the rectangle must be fully transparent so that it doesn't
hide the controls behind it.

In VB6 I would have achieved this using a shape control. How can I do this
in .NET?

The only workable option I've come up with so far is to use four label
controls with a coloured background, and arrange these so that each one
forms one of the four sides of the rectangle. This seems like a lot of
messing about to implement though. Is there a simpler way I can achieve
this?

TIA,
 
T

Tom Shelton

To assist my users in identifying where a dragged control will be dropped on
my WinForms form, I wish to be able to draw a transparent box on the top of
my form which will surround the area into which the drop will take place.
Essentially I just need to be able to draw a rectangle on top of my form,
but the inside of the rectangle must be fully transparent so that it doesn't
hide the controls behind it.

In VB6 I would have achieved this using a shape control. How can I do this
in .NET?

The only workable option I've come up with so far is to use four label
controls with a coloured background, and arrange these so that each one
forms one of the four sides of the rectangle. This seems like a lot of
messing about to implement though. Is there a simpler way I can achieve
this?

TIA,

Sure - System.Drawing. A form with a button in the middle, in the forms
paint event:

Private Sub Form1_Paint(ByVal sender As Obect, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim x As Integer = Me.Button1.Location.X - 20
Dim y As Integer = Me.Button1.Location.Y - 20
Dim w As Integer = Me.Button1.Size.Width + 40
Dim h As Integer = Me.Button1.Size.Height + 40

e.Graphics.DrawRectangle (Pens.Black, New Rectangle(x, y, w, h))
End Sub

HTH
 
M

mnadeem.abbasi

use

Dim recBackground As RectanglerecBackground = New Rectangle(10, 10,
100, 100)Dim lgb As New LinearGradientBrush(recBackground, _
Color.White,Color.GhostWhite,
LinearGradientMode.Vertical)e.Graphics.FillRectangle(lgb,
recBackground)
 
O

\(O\)enone

Tom said:
Sure - System.Drawing. A form with a button in the middle, in the
forms paint event:
[...]

Thanks to you and to mnadeem.abbasi for the quick responses! However there
was one critical element which I now realise I didn't make clear in my
original post: I need the rectangle to appear in front of any controls on my
form. The code you provided works perfectly for drawing rectangles on the
form itself, but if there are controls on the form the rectangle appears
behind them.

Is there a way to achieve this effect that will display in front of form
controls?

Many thanks,
 
H

Harry

(O)enone said:
To assist my users in identifying where a dragged control will be dropped
on my WinForms form, I wish to be able to draw a transparent box on the
top of my form which will surround the area into which the drop will take
place. Essentially I just need to be able to draw a rectangle on top of my
form, but the inside of the rectangle must be fully transparent so that it
doesn't hide the controls behind it.

In VB6 I would have achieved this using a shape control. How can I do this
in .NET?

The only workable option I've come up with so far is to use four label
controls with a coloured background, and arrange these so that each one
forms one of the four sides of the rectangle. This seems like a lot of
messing about to implement though. Is there a simpler way I can achieve
this?

TIA,

--
Have a look around for VB Powerpacks v 2.0 .This is a MS product and has the
line and shape controls just like VB6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top