Ivan
Ivan

Reputation: 20101

2D drawing: the button - redraw mystery

I have the interesting task of doing some graphs using VB.NET. So far, everything that I´ve been reading about GDI+ and e.graphics whatever is really weird. All I want to do is

1) Calculate some coordinates clicking button 1

2) Click button 2 to draw a line with the numbers from button 1

3) Click button 1 to get new coordinates

4) click button 2 to draw the previous line AND the new line.

5) click button 3 to clear the graph.

So I decided to draw everthing on top of a Panel, called panel1. I have a routine that draws on screen called drawlines,

Private Sub drawlines(ByVal g As Graphics, ByVal c As Color)
  Dim p As New Pen(c, 1)
  g.DrawLine(p, xStart, yStart, xEnd, yEnd)
  p.Dispose()
End Sub

and the other routines:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  'AddHandler Panel1.Paint, AddressOf DrawLine
  GraphicsHandler = Panel1.CreateGraphics
End Sub

Private Sub drawlines(ByVal g As Graphics, ByVal c As Color)
  Dim p As New Pen(c, 1)
  g.DrawLine(p, xStart, yStart, xEnd, yEnd)
  p.Dispose()
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  'GraphicsHandler = Panel1.CreateGraphics
  GraphicsHandler.DrawLine(myPen, 10, 10, 200, 100)
End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
  xStart = CInt(Math.Ceiling(Rnd() * 200))
  yStart = CInt(Math.Ceiling(Rnd() * 100))
  xEnd = CInt(Math.Ceiling(Rnd() * 200))
  yEnd = CInt(Math.Ceiling(Rnd() * 100))
  Me.Panel1.Invalidate()
End Sub

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Panel1.Paint
  drawlines(e.Graphics, Color.Blue)
End Sub

Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
  GraphicsHandler.Clear(Color.White)
End Sub

So far, only using the GraphicsHandler thing is working, but everytime I try to minimize the window or draw a new line, the previous lines are erased. Can some kind soul explain to me proper way to do the simple 1-5 above? For example, how can I call drawlines()from the button?

Upvotes: 0

Views: 1230

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

.NET implements a completely new graphics manipulation model with WinForms and GDI+. Your old VB6 skills will not serve you well in this brave new world.

So start by ditching the GraphicsHandler. ALL your painting should be done via the Panel's Paint event.

You should store each line in an array or List(Of Point) at the form level. Then each time the Paint event is called you draw all your lines again. .NET controls DO NOT REMEMBER their graphical state from one Paint event to the next, unlike VB6 controls.

If you need to force a redraw at the end of your button Click event you would call your Panel.Invalidate()

PSEUDOCODE:

Private myCoordinates As List(Of Point) = New List(Of Point)

Sub Button_click(sender, e)
  '' Store new coordinate
  myCoordinates.Add(New Point(x, y))
  myCoordinates.Add(New Point(x, y))
  myPanel.Invalidate()
End Sub


Sub Panel_Paint(sender, e) Handles myPanel.Paint
  For tIndex As Int32 = 0 To myCoordinates.Count - 1 Step 2
    e.graphics.DrawLine(myCoordinate(tIndex), myCoordinates(tIndex+1))
  Next
End Sub

Upvotes: 1

Related Questions