How to Create an On-Screen Dimension Calculator in Visual Basic

Submitted by: 
Language: 
Visitors have accessed this post 1677 times.

Introduction:
Welcome to my tutorial on how to create a on-screen dimensions calculator.

Steps of Creation:
Step 1:

First we want to create a form with a button to begin the process. We also want to make two labels, one to say "Console:" and label2 to contain the current console status, set the default to "Idle...". We then want to Import System.Threading because we will be using a one second delay multiple times. If we used a one second delay within the main program thread the entire program UI would freeze.

  1. Imports System.Threading

Step 2:
Now that we have imported we are ready to create a new thread, on button1 click lets create a new thread and give it the starting address of a new custom function called getx...

  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. Dim trd As thread = New thread(AddressOf getx)
  3. trd.isbackground = True
  4. trd.start()
  5. End Sub

We also want to disable checking for illegal crossthread calls on form load otherwise we would get an error when trying to update our console line in the UI from a new thread.

  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2. CheckForIllegalCrossThreadCalls = False
  3. End Sub

Step 3:
Now we want to tell the user to position their mouse in the correct location and give them a few seconds to perform the step. We then take note of the x and y co-ordinates of the mouse. (twice).

  1. Private Function getx()
  2. Dim i As Integer = 3
  3. Do Until i <= 0
  4. Label2.Text = "Position mouse over the top, left corner of the image... {" & i & " Seconds}"
  5. thread.sleep(1000)
  6. i -= 1
  7. Loop
  8. Dim x1 As Integer = MousePosition.X
  9. Dim y1 As Integer = MousePosition.Y
  10. i = 3
  11. Do Until i <= 0
  12. Label2.Text = "Position mouse over the bottom, right corner of the image... {" & i & " Seconds}"
  13. thread.sleep(1000)
  14. i -= 1
  15. Loop
  16. End Function

Step 4:
Once we have the top (left and right) and bottom (left and right) positions of the image, we want to create a new couple of integers which has the biggest co-ords minus the lowest co-ords (get the size of the image). We then output the result in a message. Done!

  1. Dim x2 As Integer = MousePosition.X
  2. Dim y2 As Integer = MousePosition.Y
  3. Dim x3 As Integer = x2 - x1
  4. Dim y3 As Integer = y2 - y1
  5. MsgBox("On-screen dimensions of the image are: " & x3 & "x" & y3)

Project Complete!
Below is the full source code and download to the project files.

  1. Imports System.Threading
  2. Public Class Form1
  3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4. Dim trd As thread = New thread(AddressOf getx)
  5. trd.isbackground = True
  6. trd.start()
  7. End Sub
  8.  
  9. Private Function getx()
  10. Dim i As Integer = 3
  11. Do Until i <= 0
  12. Label2.Text = "Position mouse over the top, left corner of the image... {" & i & " Seconds}"
  13. thread.sleep(1000)
  14. i -= 1
  15. Loop
  16. Dim x1 As Integer = MousePosition.X
  17. Dim y1 As Integer = MousePosition.Y
  18. i = 3
  19. Do Until i <= 0
  20. Label2.Text = "Position mouse over the bottom, right corner of the image... {" & i & " Seconds}"
  21. thread.sleep(1000)
  22. i -= 1
  23. Loop
  24. Dim x2 As Integer = MousePosition.X
  25. Dim y2 As Integer = MousePosition.Y
  26. Dim x3 As Integer = x2 - x1
  27. Dim y3 As Integer = y2 - y1
  28. MsgBox("On-screen dimensions of the image are: " & x3 & "x" & y3)
  29. End Function
  30.  
  31. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  32. CheckForIllegalCrossThreadCalls = False
  33. End Sub
  34. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.