Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

'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)
       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

Edited by divil
Posted

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

Howzit??
  • *Experts*
Posted

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:

 

'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

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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

  • *Experts*
Posted

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.

 

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...