CreateGraphics help in VB.net

B

bromptonville-un

Hello,

I'm an amateur in VB6, and I just switched to VB.Net (2003) .
However, I've never used graphicals functions in neither those
language... (Last time I drew something was in VB6).

I just tried the following code and it is not working properly:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim g As Graphics = Form1.CreateGraphics
Dim myPen As New Pen(Color.Red)
myPen.Width = 5
g.DrawLine(myPen, 1, 1, 45, 65)
End Sub

I get the error: "Reference to a non-shared member requires an object
reference."; I don't understand what it means.

I can replace the first line with:
Dim g As Graphics = Me.CreateGraphics
and it works... But I'd like to specify directly the Form1. (I must
also confess I don't perfectly understand the use of the "ME"
keyword...)

Any help would be appreciated.

Thank you.
 
C

CMoya

"Me" is the way to go. Use it.

Lose the idea that "Form1" "Form2" refer to instantiated objects. The error
message is pretty clear if you think about it. In VB.NET "Form1" doesn't
refer to an object. It refers to a Type.
 
F

Family Tree Mike

CMoya is correct regarding the form object. VB 6 had a hidden way of using
Form1 as an instance.

To add what CMoya said, I think that once past that issue, you will have
trouble with your direction. Drawing graphics should be done on the paint
methods of the form. You are bound to run into problems trying to draw in a
button click. Instead, you should look into handling the form's paint event,
and when the button is clicked, set some parameter that determines how you
paint handler will behave. MSDN has an example at
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.paint(VS.80).aspx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top