Results 1 to 4 of 4

Thread: enlarging and reducing the size of a circle using a scrollbar

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    71

    enlarging and reducing the size of a circle using a scrollbar

    Hi all,

    Im trying to enlarge a circle on a form using a scrollbar. I have managed to do this using the following code:

    Code:
    Private Sub DrawCircle(ByVal cp As Point, ByVal radius As Integer)
            Dim gr As Graphics
            gr = Panel1.CreateGraphics
            Dim rect As Rectangle = New Rectangle(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius)
            'gr.DrawEllipse(Pens.Black, rect)
            gr.DrawEllipse(Pens.Black, rect)
            gr.FillEllipse(Brushes.Red, rect)
            gr.Dispose()
        End Sub
    
    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            DrawCircle(New Point(TrackBar1.Value / 2, TrackBar1.Value / 2), TrackBar1.Value)
    
        End Sub
    Thing is the code draws a circle on every scroll movement. You end up with a mass of circles. I want it to clear previous graphic and just show the circle that relates to the current scrollbar graphic, giving the impression of enlarging and shrinking. Any ideas? All help most appreciated.

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: enlarging and reducing the size of a circle using a scrollbar

    Hi.

    You need to "clear" the graphics before you draw again, using:
    gr.Clear(Color)

    Code:
        Private Sub DrawCircle(ByVal cp As Point, ByVal radius As Integer)
            Dim gr As Graphics
            gr = Panel1.CreateGraphics
            gr.Clear(Panel1.BackColor)
            Dim rect As Rectangle = New Rectangle(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius)
            'gr.DrawEllipse(Pens.Black, rect)
            gr.DrawEllipse(Pens.Black, rect)
            gr.FillEllipse(Brushes.Red, rect)
            gr.Dispose()
        End Sub
    
        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            DrawCircle(New Point(TrackBar1.Value / 2, TrackBar1.Value / 2), TrackBar1.Value)
        End Sub

  3. #3
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: enlarging and reducing the size of a circle using a scrollbar

    actually it's because he's declaring a new Graphics object each time, hence the multitude of circles. You need to declare the graphics object once, and maintain it.

    Dim gr As Graphics is global

    Then in your DrawCircle method, you would replace the "Dim gr As Graphics" line with "gr.Clear()"

  4. #4
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: enlarging and reducing the size of a circle using a scrollbar

    Yeah, but for the simplicity I just said that. While were at it anyways:
    I recommend you to set the following line for the panel to true:
    Code:
    MyBase.DoubleBuffered = True
    It would get rid of the "flashing" while drawing.
    The flashing is caused by the "clear" sub, that causes the panel to invalidate and thus redraw.

Posting Permissions

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



Click Here to Expand Forum to Full Width