• We just launched and are currently in beta. Join us as we build and grow the community.

Rubberband in a PictureBox

Pruned1212444

Tech Security Strategist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
144
Likes
66
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. Private

    Sub

    Form1_Load(

    sender As

    Object

    , e As

    System.

    EventArgs

    )

    Handles

    Me

    .

    Load
  9. RBPen =

    New

    Pen(

    Color.

    Black

    , 1.5

    )

    'Color, Width
  10. RBPen.

    DashStyle

    =

    Drawing2D.

    DashStyle

    .

    Dash
  11. End

    Sub

  12. Private

    Sub

    PictureBox1_MouseDown(

    sender As

    Object

    , e As

    System.

    Windows

    .

    Forms

    .

    MouseEventArgs

    )

    Handles

    PictureBox1.

    MouseDown
  13. PictureBox1.

    Refresh

    (

    )

    'erases previous rectangle
  14. Xstart =

    e.

    X
  15. Ystart =

    e.

    Y
  16. bRB =

    True
  17. End

    Sub

  18. Private

    Sub

    PictureBox1_MouseMove(

    sender As

    Object

    , e As

    System.

    Windows

    .

    Forms

    .

    MouseEventArgs

    )

    Handles

    PictureBox1.

    MouseMove
  19. If

    bRB Then
  20. PictureBox1.

    Refresh

    (

    )

    'erases previous rectangle

  21. Select

    Case

    e.

    X
  22. Case

    Is

    <

    0
  23. RBRectangle.

    X

    =

    0
  24. RBRectangle.

    Width

    =

    Xstart
  25. Case

    0

    To

    Xstart
  26. RBRectangle.

    X

    =

    e.

    X
  27. RBRectangle.

    Width

    =

    Xstart -

    e.

    X
  28. Case

    Xstart To

    PBWidth
  29. RBRectangle.

    X

    =

    Xstart
  30. RBRectangle.

    Width

    =

    e.

    X

    -

    Xstart
  31. Case

    Is

    >

    PBWidth
  32. RBRectangle.

    X

    =

    Xstart
  33. RBRectangle.

    Width

    =

    PBWidth -

    Xstart
  34. End

    Select

  35. Select

    Case

    e.

    Y
  36. Case

    Is

    <

    0
  37. RBRectangle.

    Y

    =

    0
  38. RBRectangle.

    Height

    =

    Ystart
  39. Case

    0

    To

    Ystart
  40. RBRectangle.

    Y

    =

    e.

    Y
  41. RBRectangle.

    Height

    =

    Ystart -

    e.

    Y
  42. Case

    Ystart To

    PBHeight
  43. RBRectangle.

    Y

    =

    Ystart
  44. RBRectangle.

    Height

    =

    e.

    Y

    -

    Ystart
  45. Case

    Is

    >

    PBHeight
  46. RBRectangle.

    Y

    =

    Ystart
  47. RBRectangle.

    Height

    =

    PBHeight -

    Ystart
  48. End

    Select

  49. PictureBox1.

    CreateGraphics

    .

    DrawRectangle

    (

    RBPen, RBRectangle)
  50. End

    If
  51. End

    Sub

  52. Private

    Sub

    PictureBox1_MouseUp(

    sender As

    Object

    , e As

    System.

    Windows

    .

    Forms

    .

    MouseEventArgs

    )

    Handles

    PictureBox1.

    MouseUp
  53. If

    bRB Then
  54. bRB =

    False
  55. 'Add code here
  56. End

    If
  57. End

    Sub

  58. Private

    Sub

    PictureBox1_Resize(

    sender As

    Object

    , e As

    System.

    EventArgs

    )

    Handles

    PictureBox1.

    Resize
  59. PBWidth =

    PictureBox1.

    Width

    -

    1

    'Right edge
  60. PBHeight =

    PictureBox1.

    Height

    -

    1

    'Bottom edge
  61. End

    Sub


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top