How to draw line on panel from button

robbremington

Member
Joined
Jan 6, 2003
Messages
17
This sub draws a line on the panel

Code:
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:
Code:
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
 
I read somewhere that the Graphics class contained in the
PaintEventArgs of a Controls 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:

Code:
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
 
Theres nothing wrong with using e.Graphics to draw directly. If you declare another variable and assign that to it, youre using exactly the same object.
 
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 Im 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
 
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.

Code:
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 dont see a need for
the function at all, unless you plan on putting more code in the
function.

Also, since the method doesnt return a value, you should declare
it as a Sub instead of a Function.
 
Back
Top