Extreme Design Studio
Extreme Design Studio Logo

How to Create a Floating Window in Visual Basic

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!

Tutorial Level: Easy

Programming Language: Visual Basic

.NET Framework: 4.0

Step 1

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

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

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

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 Source Files

Rate & Share
1 Star2 Stars3 Stars4 Stars5 Stars   5.00 out of 5 based on 2 ratings.
Tags & Keywords
Related Posts
Comments


6 − = two