Question Drawing & Lassoo

bish

Member
Joined
Dec 30, 2008
Messages
14
Programming Experience
Beginner
Ok hello everyone, the last time l used VB was version4!! things have certainly changed since those days, l have a question and lm hoping someone out there can get me on the right footing.....

I wish to do three things

1) draw three letter's 'P' 'B' & 'C' on a form (at a later date mutiples of each wil be added)
2) to be able to drag them anywhere on the form
3) to be able to lassoo them and drag them all at the same time.

Is anyone out there upto the challenge in getting me started?

Many Thanks in advance

Paul Bishop
 
Ive think ive started the first bit, now how do l drag each one?

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

e.Graphics.DrawString("B", Me.Font, Brushes.Blue, 10, 10)
e.Graphics.DrawString("P", Me.Font, Brushes.Blue, 10, 20)
e.Graphics.DrawString("C", Me.Font, Brushes.Blue, 10, 30)

End Sub


Anyone?

Paul Bishop
 
The first thing is that you'll need to store the locations of the three letters in member variables rather than hard-coding them in the Paint event handler. That way their values can be changed and the next time the form is repainted the new locations will be used.

Now, your letters are not objects so there's no way to handle any events associated with them directly. You'd need to handle the MouseDown event of the form and then test whether it was within the area occupied by a letter. It's up to you to determine how that area is defined. If it is then you'd set a flag to indicate that a dragging operation was in progress. You'd then handle the MouseMove event of the form and, if dragging was in progress, you'd change the location of the letter(s) being dragged by the amount the mouse had moved.

Now, when a letter has moved you'd need to force the form to repaint. You can do this by calling its Refresh method. That's a bit of a brute force approach though and can lead to flickering because painting is quite slow. A better option would be to call Invalidate and Update, so you can specify as small an area as possible to repaint. You'd calculate the smallest area that contained the letter before it was moved and invalidate that, then do the same for area containing the letter after it was moved. Finally you'd call Update to repaint the area you invalidated.
 
Back
Top