calling subs

davco

Member
Joined
Sep 13, 2006
Messages
8
Programming Experience
Beginner
Why can`t I get this to execute in the button1_click event?


VB.NET:
PublicSub AddCurveExample(ByVal e As PaintEventArgs)
' Create some points.
Dim point1 AsNew Point(20, 20)
Dim point2 AsNew Point(40, 0)
Dim point3 AsNew Point(60, 40)
Dim point4 AsNew Point(80, 20)
' Create an array of the points.
Dim curvePoints As Point() = {point1, point2, point3, point4}
' Create a GraphicsPath object and add a curve.
Dim myPath AsNew GraphicsPath
myPath.AddCurve(curvePoints, 0, 3, 0.8F)
' Draw the path to the screen.
Dim myPen AsNew Pen(Color.Black, 2)
e.Graphics.DrawPath(myPen, myPath)
EndSub
 
 
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddCurveExample()
EndSub
 
Last edited by a moderator:
You are not passing the argument

You have setup your sub "AddCurveExample to expect arguments, therefore you can no call itwith out supplying them. You would need to call the sub like this



VB.NET:
PublicSub AddCurveExample(ByVal e As PaintEventArgs)
' Create some points.
Dim point1 AsNew Point(20, 20)
Dim point2 AsNew Point(40, 0)
Dim point3 AsNew Point(60, 40)
Dim point4 AsNew Point(80, 20)
' Create an array of the points.
Dim curvePoints As Point() = {point1, point2, point3, point4}
' Create a GraphicsPath object and add a curve.
Dim myPath AsNew GraphicsPath
myPath.AddCurve(curvePoints, 0, 3, 0.8F)
' Draw the path to the screen.
Dim myPen AsNew Pen(Color.Black, 2)
e.Graphics.DrawPath(myPen, myPath)
EndSub
 
 
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim x as new painteventargs('you will need to populat this par t accordingly
)
AddCurveExample(x)
EndSub
 
Last edited by a moderator:
Thanks for the help but I just couldn`t get it to work.I keep getting the error:Object reference not set to an instance of an object.
 
You should not be creating your own paint event args object. If you want to do some painting then override the Paint event and pass the graphics object supplied in the parameter which is usually e.

Moved to a more appropriate forum
 
Code Box

From what I can tell you are looking to do, overwriting the paint arg is not the write solution for this. Keep in mind you should be using a rectangle to contain this, but this should still work.


VB.NET:
PublicSub AddCurveExample(ByVal g as system.Drawing.Graphics) 
' Create some points.
Dim point1 AsNew Point(20, 20)
Dim point2 AsNew Point(40, 0)
Dim point3 AsNew Point(60, 40)
Dim point4 AsNew Point(80, 20)
' Create an array of the points.
Dim curvePoints As Point() = {point1, point2, point3, point4}
' Create a GraphicsPath object and add a curve.
Dim myPath AsNew GraphicsPath
myPath.AddCurve(curvePoints, 0, 3, 0.8F)
' Draw the path to the screen.
Dim myPen AsNew Pen(Color.Black, 2)
e.Graphics.DrawPath(myPen, myPath)
EndSub
 
 
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As System.Drawing.Graphics
x = Me.CreateGraphics
AddCurveExample(x)
EndSub
 
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As System.Drawing.Graphics
x = Me.CreateGraphics
AddCurveExample(x)
EndSub

No,as i said the CreateGraphics method is not ment to be used for painting in that manner. You need to override the onpaint event..

VB.NET:
Protected Overrides Sub OnPaint(byval e as system.windows.forms.painteventargs)
MyBase.OnPaint(e)
 
AddCurveSample(e)
End Sub

Also, when you are implementing your own painting, you should set the following in the classes constructor (Sub New)

VB.NET:
Mybase.SetStyle(ControlStyles.AllPaintingInWMPaint,True)
MyBase.SetStyle(ControlStyles.UserPaint,True)
MyBase.SetStyle(ControlStyles.DoubleBuffer,True)
 
Vis781, I know you are probably 100% correct in your statment, but the part that is confusing me is that the it seems that Davco wants to initiate the draw with a button click.

If he overrides the paint event, it will not be able to be activated by the button click. I am asking for my own benefit, but I am not following how to make this work with the paint event. I agree with you 100% that when you can get the actual graphic that is always better then creating one, but can that be done in this scenerio?

Thanks.
 
All painting should be done in the paint event, but it will be up to your programming logic as to what to paint and when. for example..

VB.NET:
Private _DrawACurve as boolean = False

Button Click...

VB.NET:
Me._DrawACurve = true
Me.Invalidate

Paint Event

VB.NET:
If Me.DrawACurve then
 
AddCurveSample(e)
 
End If
 
vinnie is right,I want to paint from the click event.
in the onpaint event I would have just forgotten the sub and just coded the painting in.

Thanks vis , That is exactly what I was looking for.
I haven`t been doing this long enough to figure some of this stuff out.
Thanks vinnie,for helping me get it across.
 
Back
Top