• .rectangle.offset? (vb.net)

    Home » Forums » Developers, developers, developers » DevOps Lounge » .rectangle.offset? (vb.net)

    Author
    Topic
    #401324

    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

    Viewing 1 reply thread
    Author
    Replies
    • #789814

      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)) – 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!

      • #790219

        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.

      • #790220

        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.

    • #789815

      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)) – 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!

    Viewing 1 reply thread
    Reply To: .rectangle.offset? (vb.net)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: