Create a Floating Window in Visual Basic

Posted on January 25, 2013 by Dan Doicaru in Visual Basic Category with Easy Difficulty

In this tutorial we will learn how to create a simple moving window in Visual Basic using Visual Studio 2010 and some easy techniques. Good luck!

Programming Language: Visual Basic

.NET Framework: 4.0

Step 1 - Create a New Visual Basic Project

Create a new project by going to File - New - Project (CTRL + SHIFT + N).

Select Visual Basic - Windows Forms Application and create your project.

step1

Step 2 - Add a Timer

From Toolbox (CTRL + ALT + X) search for Timer (from Components Category) and drag and drop to your form.

step2

Now click on Timer1 and on the right of your screen you should have Properties Panel (F4).

Set the options below:

  • Enabled = True
  • Interval = 50

step3

Step 3 - Adding the Code to Timer

Now double click on Timer1 to set some actions for our window.

You will be redirected to Code Page, inside your Double Click Function.

To make your window float or move diagonally just write the following code inside your function:

Me.SetBounds((Me.Bounds.X + 1), (Me.Bounds.Y + 1), Me.Width, Me.Height)

Your entire code should look like this:

Public Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
	Me.SetBounds((Me.Bounds.X + 1), (Me.Bounds.Y + 1), Me.Width, Me.Height)
End Sub

End Class

Code explanation:

  • Me.<property> - used to make reference to the current window you are working in
  • SetBounds(Integer X, Interger Y, Integer Width, Integer Height) - used to set window x/y location and also width or height
  • Me.Bounds.X - gets the initial X Position of the window

Final Results - A Moving Window

floating-window

Now your window will move diagonally infinitely until you stop it.

If you want to stop the window, you need to add a conditional statement to check the Y Location or X Location and where to stop.

See the example below:

Public Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
	If (Me.Bounds.Y > 300) Then
		Me.SetBounds((Me.Bounds.X + 0), (Me.Bounds.Y + 0), Me.Width, Me.Height)
	Else
		Me.SetBounds((Me.Bounds.X + 1), (Me.Bounds.Y + 1), Me.Width, Me.Height)
	End If
End Sub

End Class

Now your window will stop if his Y Position will be higher than 300.

Download Files

Rate & Share
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
Related Articles
Comments
Hint: Wrap your code syntax (html, css or others) between <pre> and </pre> tags. All comments are moderated.


3 + = 5

Search
Subscribe by E-mail

HTML-TUTS is created and maintained by Dan Doicaru.
This website is a rebrand to Extreme Design Studio (extremestudio.ro), built under WordPress platform.