• Very noob problem with GDI rendering in VB
    6 replies, posted
I've decided to expand my small VB knowledge to GDI drawing, since it could be useful sometime in the future. Now I've run into a very annoying problem which I can't find no answer for, and has stressed me out to the point that I've got a "Fuck everything!" attitude. This is the code, taken from MSDN. And set up as I hope it should be. [CODE]Public Class Form1 'Declaration Public Sub DrawLine( _ ByVal pen As Pen, _ ByVal pt1 As Point, _ ByVal pt2 As Point _ ) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DrawLinePoint() End Sub Private Sub DrawLinePoint(ByVal e As PaintEventArgs) ' Create pen. Dim blackPen As New Pen(Color.Black, 3) ' Create points that define line. Dim point1 As New Point(100, 100) Dim point2 As New Point(200, 100) ' Draw line to screen. e.Graphics.DrawLine(Pens.AliceBlue, point1, point2) End Sub End Class[/CODE] The problem is with the [CODE] Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DrawLinePoint() End Sub[/CODE] It keeps returning with: [CODE]Error 1 Argument not specified for parameter 'e' of 'Private Sub DrawLinePoint(e As System.Windows.Forms.PaintEventArgs)'.[/CODE] Can somebody help me out with this very noob problem so I can at least begin [B]learning[/B] how to use GDI? I'm using Visual Basic 2010 Professional.
[QUOTE=cdlink14;23214631] [CODE] Private Sub DrawLinePoint(ByVal e As PaintEventArgs) ' Create pen. Dim blackPen As New Pen(Color.Black, 3) ' Create points that define line. Dim point1 As New Point(100, 100) Dim point2 As New Point(200, 100) ' Draw line to screen. e.Graphics.DrawLine(Pens.AliceBlue, point1, point2) End Sub [/CODE] [CODE] Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DrawLinePoint() End Sub[/CODE] [CODE]Error 1 Argument not specified for parameter 'e' of 'Private Sub DrawLinePoint(e As System.Windows.Forms.PaintEventArgs)'.[/CODE][/QUOTE] Look at the function declaration (first code box), then look at how you are calling it (second code box). There is no function DrawLinePoint(), but there is a function called DrawLinePoint(ByVal e As PaintEventArgs). The worst bugs are always the ones that are overlooked, this kind of thing has happened to me so many god damn times.
Ok, so I changed it to say: [CODE]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DrawLinePoint(e) End Sub[/CODE] assuming it's correct, fixed the error, but now when I run the code, the line isn't being draw at all. I've ensured it's a visible color, but still no result... code from the last box as currently is: [CODE] Private Sub DrawLinePoint(ByVal e As PaintEventArgs) ' Create pen. Dim blackPen As New Pen(Color.Black, 16) ' Create points that define line. Dim point1 As New Point(100, 100) Dim point2 As New Point(300, 100) ' Draw line to screen. e.Graphics.DrawLine(blackPen, 10, 10, 15, 15) End Sub End Class[/CODE]
Which MSDN example did you take it from? The reason it doesn't work is that in this line: [code]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load[/code]...you can see that it passes in 'e' as the type System.EventArgs. However, in DrawLinePoint: [code]Private Sub DrawLinePoint(ByVal e As PaintEventArgs)[/code]It's expecting [B]Paint[/B]EventArgs. It doesn't cause an error when you compile because PaintEventArgs is derived from EventArgs, but it doesn't draw anything because PaintEventArgs has extra stuff in it that isn't set in Form1_Load. So when you try to reference e.Graphics, it runs into problems. To fix this, you need to call DrawLinePoint from a place where it can get a proper PaintEventArgs object. I haven't touched .NET stuff in awhile so I'm just going from memory, but if you look at the Events list for the form you created, there should be something like a Paint event. You can try handling that event then calling your Sub there. There might be other things you need to do, but that's a step in the right direction. More info: [url]http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx[/url]
I found an even easier way of doing it. [CODE]Public Class Form1 Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown 'Declarations Dim blackpen As New Pen(Color.Black, 16) 'Draw Graphic Me.CreateGraphics.DrawLine(blackpen, 10, 10, 300, 300) End Sub End Class[/CODE] the only problem is, it needs to be triggered to appear, after the form has loaded!
[code] Me.Invalidate() [/code]
Sorry to bump an old thread, but I don't want to create a new thread for this small question. I've basically created a rectangle using this code: [code]Dim greyboard As New Rectangle greyboard.X = 6 greyboard.Y = 6 greyboard.Width = 230 greyboard.Height = 706 Me.CreateGraphics.DrawRectangle(Pens.slategrey, greyboard)[/code] This just creates the rectangle outline, how would I go about making it solid ( or filling it )? [editline]06:55PM[/editline] EDIT: FIXED! [CODE] Dim greyboard As New Rectangle greyboard.X = 6 greyboard.Y = 6 greyboard.Width = 230 greyboard.Height = 706 Me.CreateGraphics.FillRectangle(Brushes.SlateGray, greyboard) Me.CreateGraphics.DrawRectangle(Pens.SlateGray, greyboard)[/CODE] Thanks to Bean, who helped me on steam chat, after seeing the message here!
Sorry, you need to Log In to post a reply to this thread.