Skip to main content

Rubberband in a PictureBox

This program was created in Microsoft Visual Basic 2010 Express to demonstrate how to draw a rubberband rectangle in a picture box.

After creating a Windows Forms Applications, resize Form1 to 500 x 500. Add a PictureBox control to Form1 and resize to fill the Form workspace. Change the following PictureBox1 Properties

Anchor - select Top, Bottom, Left, & Right
BackColor - White

You will use the Form1 event Load to enter the initial properties of the Pen. In this example, the Rubberband will be a dashed black line with a line width of 1.5.

Picturebox1 events used are Mouseown, MouseMove, MouseUp and Resize. When you press the mouse button on the Picturebox the starting position will be saved in Xstart and Ystart. Picturebox1.Refresh is used to clear any previous drawings.

As the mouse is moved across the Picturebox, the location will be updated and a new rectangle is drawn using the DrawRectangle subroutine. Depending on the direction the mouse is moving with respect to the starting point will determine the values used in the RBrectangle variable.

When the mouse button is released, the process is stopped by setting bRB as false. You can add code in this area to do something with the area that is rubberbanded.

If you resize the Form, the boundaries of the Picturebox are updated. In order to keep the rectangle visible along the right and bottom edges, you will need to reduce the values of PBWidth and PBHeight by 1.

  1. Dim Xstart As Integer
  2. Dim Ystart As Integer
  3. Dim PBWidth As Integer
  4. Dim PBHeight As Integer
  5. Dim bRB As Boolean
  6. Dim RBPen As Pen
  7. Dim RBRectangle As Rectangle
  9. Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  10. RBPen = New Pen(Color.Black, 1.5) 'Color, Width
  11. RBPen.DashStyle = Drawing2D.DashStyle.Dash
  12. End Sub
  14. Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  15. PictureBox1.Refresh() 'erases previous rectangle
  16. Xstart = e.X
  17. Ystart = e.Y
  18. bRB = True
  19. End Sub
  21. Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
  22. If bRB Then
  23. PictureBox1.Refresh() 'erases previous rectangle
  25. Select Case e.X
  26. Case Is < 0
  27. RBRectangle.X = 0
  28. RBRectangle.Width = Xstart
  29. Case 0 To Xstart
  30. RBRectangle.X = e.X
  31. RBRectangle.Width = Xstart - e.X
  32. Case Xstart To PBWidth
  33. RBRectangle.X = Xstart
  34. RBRectangle.Width = e.X - Xstart
  35. Case Is > PBWidth
  36. RBRectangle.X = Xstart
  37. RBRectangle.Width = PBWidth - Xstart
  38. End Select
  40. Select Case e.Y
  41. Case Is < 0
  42. RBRectangle.Y = 0
  43. RBRectangle.Height = Ystart
  44. Case 0 To Ystart
  45. RBRectangle.Y = e.Y
  46. RBRectangle.Height = Ystart - e.Y
  47. Case Ystart To PBHeight
  48. RBRectangle.Y = Ystart
  49. RBRectangle.Height = e.Y - Ystart
  50. Case Is > PBHeight
  51. RBRectangle.Y = Ystart
  52. RBRectangle.Height = PBHeight - Ystart
  53. End Select
  55. PictureBox1.CreateGraphics.DrawRectangle(RBPen, RBRectangle)
  56. End If
  57. End Sub
  59. Private Sub PictureBox1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  60. If bRB Then
  61. bRB = False
  62. 'Add code here
  63. End If
  64. End Sub
  66. Private Sub PictureBox1_Resize(sender As Object, e As System.EventArgs) Handles PictureBox1.Resize
  67. PBWidth = PictureBox1.Width - 1 'Right edge
  68. PBHeight = PictureBox1.Height - 1 'Bottom edge
  69. End Sub

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 for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


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

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.