What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a shaped form by setting its region in Visual Basic .NET
DescriptionThis example shows how to make a shaped form by setting its region in Visual Basic .NET.
Keywordsforms, region, set form region, shaped form, TransparencyKey, Visual Basic .NET, VB.NET
CategoriesControls, Software Engineering

In theory you can set a form's TransparencyKey property to make parts of the form disappear. In practice, this feature has gotten buggy in recent versions of .NET so it doesn't always work consistently on every computer.

An alternative is to create a region that defines the form's shape and then set the form's Region property to the region.

The example program uses the following code to give its form a star shape.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make points to define a polygon for the form.
    Dim pts(0 To 9) As PointF
    Dim cx As Single = Me.ClientSize.Width * 0.5
    Dim cy As Single = Me.ClientSize.Height * 0.5
    Dim r1 As Single = Me.ClientSize.Height * 0.45
    Dim r2 As Single = Me.ClientSize.Height * 0.25
    Dim theta As Single = -PI / 2
    Dim dtheta As Single = 2 * PI / 10
    For i As Integer = 0 To 9 Step 2
        pts(i) = New PointF( _
            cx + r1 * Cos(theta), _
            cy + r1 * Sin(theta))
        theta += dtheta
        pts(i + 1) = New PointF( _
        cx + r2 * Cos(theta), _
        cy + r2 * Sin(theta))
        theta += dtheta
    Next i

    ' Use the polygon to define a GraphicsPath.
    Dim path As New GraphicsPath()

    ' Make a region from the path.
    Dim form_region As New Region(path)

    ' Restrict the form to the region.
    Me.Region = form_region
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.