How to draw line on panel from button

robbremington

Newcomer
Joined
Jan 6, 2003
'This sub draws a line on the panel

Visual Basic:
Private Sub Panel1_Paint(ByVal sender As System.Object, 
       ByVal  e  As System.Windows.Forms.PaintEventArgs) 
                                                       Handles  Panel1.Paint
        Dim blackPen As New Pen(Color.Black, 3)
        e.Graphics.DrawLine(blackPen, 50, 100, 150, 200)
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, 
                ByVal e As System.EventArgs) Handles Button1.Click
        'What CODE do I put here to also draw a line on the 
         PANEL??????
    End Sub
 
Last edited by a moderator:

Cywizz

Centurion
Joined
Jan 31, 2003
Location
JHB, South Africa
Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles Button1.Click
'force the paint event of the panel to fire
 Panel1.Refresh()
End Sub
Hope it helped
 

Bucky

Contributor
Joined
Dec 23, 2001
Location
East Coast
I read somewhere that the Graphics class contained in the
PaintEventArgs of a Control's Paint event cannot be accessed
directly, and that you need to declare a new Graphics class and
set it to the argument passed, then use THAT variable.

So, your paint code should be this:

Visual Basic:
'This sub draws a line on the panel
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim blackPen As New Pen(Color.Black, 3)
Dim g As Graphics = e.Graphics

g.DrawLine(blackPen, 50, 100, 150, 200)

' Free up memory
g.Dispose()
End Sub
 

divil

Ultimate Contributor
Joined
Nov 17, 2002
Location
England
There's nothing wrong with using e.Graphics to draw directly. If you declare another variable and assign that to it, you're using exactly the same object.
 

robbremington

Newcomer
Joined
Jan 6, 2003
How to draw many lines on panel

:confused: :confused:
Doing a Panel1.refresh only seems to paint the last line.
Note: this is a simplified example of a complex app that generates a flowchat of TextBoxes joined by lines. In VB6 the line control was used, now I'm very confused!!

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
DrawLineOnPanel1(20, 40, 60, 80) 'Draw one line
DrawLineOnPanel1(40, 60, 80, 100) 'Draw a second line
'......
DrawLineOnPanel1(40, 60, 80, 100) 'Draw a 100th line

End Sub
Public Function DrawLineOnPanel1(ByVal inXp As Short,
ByVal inYp As Short, ByVal inXd As Short, ByVal inYd
As Short) As Boolean
'What code goes here???
End Function
 

Bucky

Contributor
Joined
Dec 23, 2001
Location
East Coast
You need to pass the Graphics class of the PaintEventArgs into
your DrawLineOnPanel1 and pass it ByRef instead of ByVal, so you
can modify the Graphics class directly and draw with it.

Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles Button1.Click
DrawLineOnPanel1(20, 40, 60, 80, e.Graphics) 'Draw one line
' etc
End Sub

Public Sub DrawLineOnPanel1(ByVal inXp As Short, ByVal inYp As Short, ByVal inXd As Short, ByVal inYd As Short, ByVal g As Graphics)
g.DrawLine(New Point(inXp, inYp), New Point(inXd, inYd))
End Sub

In fact, since the call to the Graphics.DrawLine() method is still
one line of code with the same parameters, I don't see a need for
the function at all, unless you plan on putting more code in the
function.

Also, since the method doesn't return a value, you should declare
it as a Sub instead of a Function.
 
Top Bottom