Rubberband in a PictureBox

Submitted by: 
Visitors have accessed this post 2393 times.

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
  8.  
  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
  13.  
  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
  20.  
  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
  24.  
  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
  39.  
  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
  54.  
  55. PictureBox1.CreateGraphics.DrawRectangle(RBPen, RBRectangle)
  56. End If
  57. End Sub
  58.  
  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
  65.  
  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

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.