Elfoc
Elfoc

Reputation: 3689

How to start Picturebox events from button

I have button named "Paint" which should allow drawing rectangle on my picturebox after a click, i.e. it acts like a switch to allow drawing(on/off).

I've drawn rectangle using mouse positions as explained here: How Can I Capture Mouse Coordinate on PictureBox? . But rectangle is drawn whenever I move over PictureBox.

How can I implement the functionality where drawing must be implemented only when "Paint" is 'on' I've tried starting implementation from events of Picturebox: Paint, MouseDown, MoseMove, Mouse Up...

Upvotes: 0

Views: 1173

Answers (2)

Sara S.
Sara S.

Reputation: 1375

  1. set a flag(bool) in your application telling you the mode you are in whether drawing or not(can be activated from the button you are telling about).
  2. in mouse down take the start point(e.x, e.y) from the mouse event handler. now you have the top left point of the rectangle.

    3.while mouse move take e.x and e.y and which is the bottom right point and draw your rectangle. put the drawing code in mouse move so that the it draws like the "Paint" Program(do this if the draw flag is true).

  3. in mouse up reset the drawing flag

    5.in the paint event of the picturebox draw the all the shapes you have so that if you minimized your application windows and then maximized it you will find your shapes drawn this can be achieved by making the rectangle is a class and make some instances of it(for loop over your shapes and draw it).

Upvotes: 2

Tigran
Tigran

Reputation: 62256

Simplest solution is to add a boolean bDraw variable, which becomes TRUE only on button click. All other drawing methods do not do anything if this variable is FALSE.

Other solution could be simply to subscribe to Mouse events inside button click event handler. So if button is NOT clicked, no event raise happen.

Upvotes: 0

Related Questions