Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Sep 2002
    Location
    Gainesville, Florida
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    .rectangle.offset? (vb.net)

    I'm trying to learn vb.net, the problem i'm having in working with the system.drawing class. I draw a rectangle and want to move it. The offset method seems to be designed to do this. but I can't seem to make it work. here's a snippet of code, any sugesstions are appreciated


    Public Class frmLights
    Inherits System.Windows.Forms.Form

    Dim grpGraphics As System.Drawing.Graphics
    Dim mrecSquare As New System.Drawing.Rectangle(32, 120, 20, 20))

    Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquare.Click
    Dim bshDraw As System.Drawing.Brush
    bshDraw = New SolidBrush(Color.Crimson)
    grpGraphics = Me.CreateGraphics
    grpGraphics.FillRectangle(bshDraw, mrecSquare)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    mrecSquare.Offset(5, 5)
    End Sub
    end class

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: .rectangle.offset? (vb.net)

    Three ideas for you:

    1) There is an extra closing paranthesis around the following line: Dim mrecSquare As New System.Drawing.Rectangle(32, 120, 20, 20)<font color=red>)</font color=red> - Remove this
    2) Move your Brush object into the Class scope [as opposed to being inside the Sub btn_Square_Click() scope]
    3) Re-call the line filling the rectangle object with color [grpGraphics.FillRectangle(bshDraw, mrecSquare)] after calling the Offset method.

    Explanation:
    The Offset method does exactly what you expect. The problem is that the offset rectangle is no longer filled since the brush (coloring the rectangle) is limited only to the first sub. Moving the brush into the class-level scope will make it persist beyond the initial sub. You will see that a result of this is that the previous rectangles stay in addition to the new, offset rectangles. If you want to get around this, simply add a Me.Refresh line before the second call to the FillRectangle method.

    Please post back with any further questions. Good luck!

  3. #3
    Lounger
    Join Date
    Sep 2002
    Location
    Gainesville, Florida
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: .rectangle.offset? (vb.net)

    thanks,
    the extra parens were a typo copying it to the forum, changing the scope works, I would preferr the square didn't leave residuals but I can work around that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •