Rubberband in a PictureBox
Submitted by Dr Dave on Wednesday, November 20, 2013 - 05:42.
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.
- Dim Xstart As Integer
- Dim Ystart As Integer
- Dim PBWidth As Integer
- Dim PBHeight As Integer
- Dim bRB As Boolean
- Dim RBPen As Pen
- Dim RBRectangle As Rectangle
- Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
- RBPen = New Pen(Color.Black, 1.5) 'Color, Width
- RBPen.DashStyle = Drawing2D.DashStyle.Dash
- End Sub
- Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
- PictureBox1.Refresh() 'erases previous rectangle
- Xstart = e.X
- Ystart = e.Y
- bRB = True
- End Sub
- Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
- If bRB Then
- PictureBox1.Refresh() 'erases previous rectangle
- Select Case e.X
- Case Is < 0
- RBRectangle.X = 0
- RBRectangle.Width = Xstart
- Case 0 To Xstart
- RBRectangle.X = e.X
- RBRectangle.Width = Xstart - e.X
- Case Xstart To PBWidth
- RBRectangle.X = Xstart
- RBRectangle.Width = e.X - Xstart
- Case Is > PBWidth
- RBRectangle.X = Xstart
- RBRectangle.Width = PBWidth - Xstart
- End Select
- Select Case e.Y
- Case Is < 0
- RBRectangle.Y = 0
- RBRectangle.Height = Ystart
- Case 0 To Ystart
- RBRectangle.Y = e.Y
- RBRectangle.Height = Ystart - e.Y
- Case Ystart To PBHeight
- RBRectangle.Y = Ystart
- RBRectangle.Height = e.Y - Ystart
- Case Is > PBHeight
- RBRectangle.Y = Ystart
- RBRectangle.Height = PBHeight - Ystart
- End Select
- PictureBox1.CreateGraphics.DrawRectangle(RBPen, RBRectangle)
- End If
- End Sub
- Private Sub PictureBox1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
- If bRB Then
- bRB = False
- 'Add code here
- End If
- End Sub
- Private Sub PictureBox1_Resize(sender As Object, e As System.EventArgs) Handles PictureBox1.Resize
- PBWidth = PictureBox1.Width - 1 'Right edge
- PBHeight = PictureBox1.Height - 1 'Bottom edge
- End Sub
Add new comment
- 592 views