Sign in to follow this  

Help with creating and calling classes please

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a slight knowledge on classes and have used them to make basic calls, however I am having a problem with a little learning project I am working on. I am using 2005 VB .NET. I just had a basic "breakout" game I created. Simply a paddle and a ball that I made bounce around the form. This however was all done in a timer event. Now I wanted to create a class to draw picture box on the screen. I went into the "windows form designer code" and found what is below in it: Friend WithEvents picBal As System.Windows.Forms.PictureBox Me.picBal = New System.Windows.Forms.PictureBox 'picBal ' Me.picBal.BackColor = System.Drawing.SystemColors.ActiveCaptionText Me.picBal.Location = New System.Drawing.Point(22, 47) Me.picBal.Name = "picBal" Me.picBal.Size = New System.Drawing.Size(14, 15) Me.picBal.TabIndex = 2 Me.picBal.TabStop = False So I figured I should be able to place this code in a seperate class, which I called clsBall and then create a new instance of the class in my form and then call to it in my code. Well that didnt work and I havent been having much luck finding something for drawing picboxes. Alot of the tutorials have been working with basic stuff. I have listed below what I have in my class right now. Any help would be greatly appreciated! TIA! ***************** Class Code ***************** Public Class clsBall Friend WithEvents picBal As System.Windows.Forms.PictureBox Public Sub drawBall() Me.picBal = New System.Windows.Forms.PictureBox Me.picBal.BackColor = System.Drawing.SystemColors.ActiveCaption Me.picBal.Location = New System.Drawing.Point(72, 6) Me.picBal.Name = "PicBal" Me.picBal.Size = New System.Drawing.Size(4, 4) Me.picBal.TabIndex = 0 Me.picBal.TabStop = False End Sub End Class Xtr33me [Edited by - xtr33me on April 5, 2006 7:26:47 PM]

Share this post


Link to post
Share on other sites
A hint: I almost didn't look at this topic because schoolwork-related questions are banned from these forums. By "class", you meant the programming kind, but I couldn't tell from the topic title.

What you want to do it leave the picturebox in the form class. Your drawing class should have a function that takes a Graphics object and draws on it. Something like this:

Public Class clsBall
Public Sub DrawBall(Graphics g)
g.DrawCircle(50, 50, 30, 40) 'or whatever
End Sub
End Class


Then you'd call it like this:

Public Sub tmr_OnTick()
MyBallClass.DrawBall(picBox1.CreateGraphics())
End Sub


Just off the top of my head, but I think that should basically work, though of course you'll want to make something meaningful inside of DrawBall. The Graphics class is really useful for this sort of thing.

Share this post


Link to post
Share on other sites
Beandog I just wanted to thank you for your reply. I am going to try it once I get off work here soon. Thanks for the tip on the subject line also..hehe...never even crossed my mind till you made it aware to me. Will use a better choice of words in the future. I just wanted to thank you. Will let ya know how it works out!

Xtr33me

Share this post


Link to post
Share on other sites
I had to type the code in for the class like below:

Public Class clsBall

Public Sub DrawBall(ByVal g As Graphics)
g.DrawEllipse(Pens.AntiqueWhite, 50, 50, 30, 40)
End Sub

End class

Then in the form I dimmed MyBallClass as a new instance of clsBall and I dimmed picbox1 as graphics. Then under the timer tick event I put:

MyBallClass.DrawBall(picBox1.CreateGraphics())

I now get an error stating "CreateGraphics() is not a member of System.Drawing.Graphics"

When I type the "." after picBox1 I get the drawElipse, etc. I am guessing that I dimmed the picBox1 incorrectly. Any ideas? Thanks!

Xtr33me

Share this post


Link to post
Share on other sites

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this