robbremington Posted February 19, 2003 Posted February 19, 2003 (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 February 19, 2003 by divil Quote
Cywizz Posted February 19, 2003 Posted February 19, 2003 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 Quote Howzit??
*Experts* Bucky Posted February 19, 2003 *Experts* Posted February 19, 2003 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 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Cywizz Posted February 19, 2003 Posted February 19, 2003 Your right Bucky... You can access it directly, but its unmanaged code (Pens, Brushes and Graphics objects) see this msdn artical about it: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnguinet/html/drguinet3_update.asp Note that the pen object must be handled the same: blackPen.Dispose() Quote Howzit??
*Gurus* divil Posted February 19, 2003 *Gurus* Posted February 19, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
robbremington Posted February 19, 2003 Author Posted February 19, 2003 Code please What would be the code then to declare a variable and call it directly?? Thanks Quote
robbremington Posted February 20, 2003 Author Posted February 20, 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 Quote
*Experts* Bucky Posted February 21, 2003 *Experts* Posted February 21, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.