Drawing lines using a button click

M

Mark

I want to use a button click event to draw a set of lines. I using the
following code:

Public Sub DrawLinesPoint(ByVal e As PaintEventArgs)

' Create pen.
Dim blackPen As New Pen(Color.Black, 1)

' Create array of points that define lines to draw.
Dim points As Point() = {New Point(0, 100), New Point(10, 100), _
New Point(200, 50), New Point(250, 300)}

'Draw lines to screen.
e.Graphics.DrawLines(blackPen, points)
End Sub



Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Me.Paint
'Will draw the lines, but not like I want.
Call DrawLinesPoint(e)
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ToolStripButton1.Click
'Does not work
Call DrawLinesPoint(e)
End Sub

Eventually I want to send parameters to the DrawLinesPoint Subroutine to
make this procedure more dynamic.

Can someone show me a simple example on how to do this?

Thanks in advance.
 
J

James Hahn

What do you mean "not like I want"?

There are many ways of drawing to a form. This is the structure that I find
most convenient:

Private buffer As Drawing.Bitmap

Public Sub DrawLinesPoint()
' Create pen.
Dim blackPen As New Pen(Color.Black, 1)
' Create array of points that define lines to draw.
Dim points As Point() = {New Point(0, 100), _
New Point(10, 100), _
New Point(200, 50), _
New Point(250, 300)}
' Create the image
buffer = New Bitmap(Me.ClientSize.Width, _
Me.ClientSize.Height, _
System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(buffer)
g.DrawLines(blackPen, points)
' Update the display
Me.Invalidate()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As
System.Windows.Forms.PaintEventArgs) _
Handles Me.Paint
If Not buffer Is Nothing Then
e.Graphics.DrawImage(buffer, 0, 0)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click
DrawLinesPoint()
End Sub
 
F

Family Tree Mike

Mark said:
I want to use a button click event to draw a set of lines. I using the
following code:

Public Sub DrawLinesPoint(ByVal e As PaintEventArgs)

' Create pen.
Dim blackPen As New Pen(Color.Black, 1)

' Create array of points that define lines to draw.
Dim points As Point() = {New Point(0, 100), New Point(10, 100), _
New Point(200, 50), New Point(250, 300)}

'Draw lines to screen.
e.Graphics.DrawLines(blackPen, points)
End Sub



Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs)
_
Handles Me.Paint
'Will draw the lines, but not like I want.
Call DrawLinesPoint(e)
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ToolStripButton1.Click
'Does not work
Call DrawLinesPoint(e)
End Sub

Eventually I want to send parameters to the DrawLinesPoint Subroutine to
make this procedure more dynamic.

Can someone show me a simple example on how to do this?

Thanks in advance.



Create a boolean flag that is toggled by the button click. In your paint
routine, only call drawlinespoint() when the flag is true. You cannot do
graphics on the form in the way you are trying to do. Your form is
repainted many times, so you need to control what is drawn for each repaint,
not just on the button click.
 
M

Mark

Hi James,
What I mean by "not like I want" is: I want to draw the lines when I click
on a button.

I want to create a routine that will draw a grid of the form when I chick on
the "Show grid" button. The .NET platform seems to require graphics to be
processed in the Paint event of the form or picture classes.
 
J

James Hahn

The sample I posted draws the lines when the button is pressed, but the
lines don't look much like a grid. To construct a grid you need to draw
vertical and horizontal lines.

The windows has to be updated in the paint event, but you can draw the
graphic anywhere at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top