Skip Navigation Links.
Form Opacity.
Language(s):VB.Net
Category(s):Winforms

One of the new features of VB.Net is the ability to make a partially (or totally) transparent form. Here is a simple example of how to do it.Note!! You must be runnint Windows 2000, XP or better for this example to work correctly.

'VB.Net Example - Form Opacity. 
'
'Note!!: You must be running Windows2000, XP or better
'for this example to work correctly. 
'
'Jon Vote 07/2002
'
'Idioma Software Inc. 
'www.idioma-software.com
'jon@idioma-software.com 
'
'This example may be freely used in code 
'as long as credit is given to the author. 
'No publication is authorized. 
'
'Code is presented as is. 
'User assumes all responsibility

'1) Start a new VB.Net project. Form1 is created by default. 
'2) Press F7 to bring up the code window. 
'3) Replace all (some? no all!) of the code in the code window 
'   with the following:
'
'VB.Net Example - Form Opacity. 
'(c) Jon Vote 07/2002
'Idioma Software Inc.
'www.idioma-software.com
'www.skycoder.com
'jon@idioma-software.com

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents hscrOpacity As System.Windows.Forms.HScrollBar
    Friend WithEvents lblOpacity As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.hscrOpacity = New System.Windows.Forms.HScrollBar()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.lblOpacity = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'hscrOpacity
        '
        Me.hscrOpacity.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.hscrOpacity.Location = New System.Drawing.Point(4, 60)
        Me.hscrOpacity.Maximum = 109
        Me.hscrOpacity.Name = "hscrOpacity"
        Me.hscrOpacity.Size = New System.Drawing.Size(376, 20)
        Me.hscrOpacity.TabIndex = 0
        Me.hscrOpacity.Value = 75
        '
        'Label1
        '
        Me.Label1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.IndianRed
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(368, 16)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Move the slider control to change the opacity property of the form."
        '
        'lblOpacity
        '
        Me.lblOpacity.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.lblOpacity.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblOpacity.ForeColor = System.Drawing.Color.IndianRed
        Me.lblOpacity.Location = New System.Drawing.Point(8, 36)
        Me.lblOpacity.Name = "lblOpacity"
        Me.lblOpacity.Size = New System.Drawing.Size(152, 16)
        Me.lblOpacity.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(384, 89)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblOpacity, Me.Label1, Me.hscrOpacity})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "www.skycoder.com - Opaque Form Example"
        Me.ResumeLayout(False)

    End Sub

#End Region

    'Initialize
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetOpacity(hscrOpacity.Value)
    End Sub

    'SetOpacity: Set the form's opacity, update display
    Private Sub SetOpacity(ByVal intValue As Integer)
        Me.Opacity = intValue / 100
        lblOpacity.Text = Me.Opacity * 100 & "% Opacity"
    End Sub

    'Update to new value
    Private Sub hscrOpacity_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles hscrOpacity.ValueChanged
        'There seems to be a bug (feature?) in the slider 
        'control. Never reaches max value.
        'I set Max to 100, but just in case 
        'it works someday...
        If sender.value > 100 Then
            sender.value = 100
        End If
        SetOpacity(sender.value)
    End Sub

End Class

This article has been viewed 4784 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.