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.
Step 2 - Add a Timer
From Toolbox (CTRL + ALT + X) search for Timer (from Components Category) and drag and drop to your form.
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
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
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.