VB Transparent Form
Judging by the title of this tutorial you’re probably asking “Are you retarded? duh all you have to do is set the opacity property for the form”
Well, yeah that is how you do it but this tutorial will take it in a different approach.
This is the one thing i love about Visual Basic, you’re not limited to just one approach, you can take several.
This tutorial, we’ll create a lightbox type of form using a module.
Nothing fancy, just a standard form with a black background and has a 50% opacity.
Throughout our program we can call the form just as easy as having another form displayed.
- Create a new Windows Forms Application
- Once created create a new module (right click on your project name in your solution explorer and choose Add -> Module)
- Give your module a name, i named mine NewWindow.vb
- Now you should have a window that has 2 lines of code
Module NewWindow
End Module
- In between the Module and End Module we will type in the following
'Declare our new form
Dim OpWindow As New Form
Public Function ShowLightbox()
'Execute code
End Function
- ShowLightbox() is what we named our function, you can name this whatever you want.
Make it as short and relevant as possible cause the name of our function is what we will be typing in when we want to call our lightbox form. - In between our Function and End Function we need to set some properties, these are the same properties you will find if you click on a form in design view and look in your properties.
ShowLightbox = Nothing
'We need to give our lightbox a name
OpWindow.Name = "Lightbox"
'We need to set the size of our lightbox_load state
OpWindow.WindowState = FormWindowState.Maximized
'We don't want to have any borders
OpWindow.FormBorderStyle = FormBorderStyle.None
'We need to determine if we want it to display in our taskbar
OpWindow.ShowInTaskbar = False
'Lets give our form a background color
OpWindow.BackColor = Color.Black
'We need to set the opacity of our window
OpWindow.Opacity = 0.5 '50% transparent
'We need to show the form when the function gets called
OpWindow.Show()
- This is all we have to do in this module
- Jump in to Form1 and add a button to your form
- In your properties set the text of the button to Exit, Close or something like that
- Click on your form and in the Properties set the property TopMost to True
TopMost = True means that this window will remain in focus weather you click off of it or not. - Double click on your Form to create a Form1_Load Event
- While you’re in your code we will create a event for our button (Button1_Click event)
- The Dropdown beside that where it says Load It will say Declarations when you choose Button, choose Click
- Now you should have to Events (form load & button click
- Inside our Form1_Load Event we will call our Lightbox form
ShowLightbox()
- If you gave your function a different name in your NewWindow module, type that in instead.
COMPLETE CODE FOR FORM_LOAD EVENT
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowLightbox()
End Sub
- Lets head back to our NewWindow Module, we will create our Close function so we can close the window when requested.
- After End Function and above End Module we will create a new function
Public Function CloseLightbox()
CloseLightbox = Nothing
OpWindow.Close()
End Function
- Now we can go back to our form1 button_click event, type in the following
CloseLightbox()
Me.Close()
- Press F5 on your keyboard or the Green play button at the top to debug your application
- You should now see your form with the lightbox effect behind it.
- If you click anywhere off your main form (Form1) Form1 still comes in focus, it’s just in inactive state, this was done when we set the TopMost = True
Here’s a example of our Form1 in Active and in Inactive state
- Now if you click the Button you will notice the Lightbox will close than Form1 will close.
So now if you want to have the Lightbox form appear like this in multiple forms all you have to do is call the ShowLightbox function and CloseLightbox function.
There are 2 reasons i did this tutorial this way
- Spares over a dozen lines of code in your Form1
- You just learned how to create a new form module.
That concludes this Visual Basic tutorial, i hope you found some interesting things here such as creating new forms via code, create functions in modules etc.
Leave a Reply
You must be logged in to post a comment.



