Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.net 4.5 need help making multiple rectangles on panel

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have this code which draws a rectangle on a panel. It works great.
but it only lets me draw one. I want to be able to draw a lot of them and also save each of their coordinates, and then be able to recreate them. Yes this is a baby drawing program.

Code:
    Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        Rect.Location = e.Location
    End Sub

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        Panel1.Cursor = Cursors.Cross
        If e.Button = MouseButtons.Left Then
            Rect.Size = New Size(e.X - Rect.X, e.Y - Rect.Y)
            Panel1.Invalidate()
        End If
    End Sub

    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim L, T, W, H As Integer
        L = Rect.X : T = Rect.Y
        W = Rect.Width : H = Rect.Height
        If W < 0 Then
            L += W : W = -W
        End If
        If H < 0 Then
            T += H : H = -H
        End If
        e.Graphics.DrawRectangle(Pens.Red, New Rectangle(L, T, W, H))
    End Sub

DougP
 
You need to use one of the overloaded versions of Invalidate, which takes a Rectangle as the parameter and only repaints that Rectangle:

Code:
    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        Panel1.Cursor = Cursors.Cross
        If e.Button = MouseButtons.Left Then
            Rect.Size = New Size(e.X - Rect.X, e.Y - Rect.Y)

            [red]Dim RectToInvalidate As Rectangle
            RectToInvalidate = New Rectangle(Rect.X, Rect.Y, Rect.Width + 1, Rect.Height + 1)
            Panel1.Invalidate(RectToInvalidate)
            RectToInvalidate = Nothing[/red]

        End If
    End Sub

I add 1 to the Width and Height of Rect in RectToInvalidate, because just using the measurements of Rect results in the right and bottom lines not to being drawn.

Also:
- any new rectangle will be "over" an old one if the two rectangles overlap, and the one underneath is obscured.
- this method only works well if you drag the cursor down and to the right. Going left and/or up, it doesn't work so well. Run the code and you'll see.

I leave these issues for you to solve.

As for saving the rectangles, you could use an array, list or collection to store rectangles, creating a new one each time the MouseDown event handler fires instead of reusing the single global Rectangle object you have now.

Hope this helps.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top