Greetings, exalted coders!
Today we will continue with our examination of classes, what they are and how they work. Let's start with the basic theory, since it seems to be a bit difficult to master, then we will move on to an example that proceeds bit by bit.
A) The Theory: ALL of Vb.net is based on Classes. Everything you see in your program starts out as a Class, EVERYTHING. That's all your Forms, all the controls you put on a form, every last bit.
Classes INHERIT from other classes. That is, they get features that other classes already have because they "derive" from those other classes. Kind of like the way I get my blue eyes & love for building things from my dad, and my love of reading and knack from gardening from my mom
You can create your own class, and give it features that the original class never dreamed of. My dad is a builder, but he never dreamed of building in code. My mom likes to garden, but HER gardens are like something from Martha Stewart. Mine look a lot more like "Little Shop of Horrors"
Classes are like blueprints: you use them to build objects, which are things you can actually see and use in your program. I'm already planning for next year's gardens; and next year, my "garden object" gets instantiated starting April 1st.
Classes can include functions, sub routines and calls to other functions and sub-routines that are inside separate modules. Because these modules are outside both your class, and whatever form you put your new object in, that new object can call those functions and subs, and DO stuff with them
OK, enough theory. Here's what I want you to do today:
A) create a new project called BasicallyClasses plus your name
B) Drag a Label onto Form1, and then lets look at its properties. A Label has lots and lots of them, including stuff like:
Text
BackColor
ForeColor
Name
Width
Height
and many many others. Some of these properties are "read only" meaning its pretty darned difficult to set them inside a Class -- but you can certainly try!
C) A label also has a lot of methods: things that a label can DO. You can see a great big honking list of them by getting into the code for your form, and setting the pulldown menu at the top left to "Label1", then opening the pulldown menu at the top right. Yup, there's craploads of stuff in there!
D) OK, now let's add a new class. Name this one "RainBowLabel" (Don't worry, you'll get to repeat this process later, and use your own name. Just humor me, m'Kay?)
E) Just below the "Public Class...." line, add an inherits line of code like so:
Inherits Windows.Forms.Label
F) Now lets look at the pulldown menus for this new class. The one at the left has an entry for "RainBowLabel events" Choose that. The one at the right will then list a bunch of stuff, including one for "Layout" Choose that one: it allows you to set some basic stuff for your label INSIDE the class.
G) Notice that when you do this, the program creates a new "Sub" for you. That's where today's code will go. Remember those properties you saw in the first Label you just drgged onto the form? Let's set some of those, like so:
Text = "Chlorophyllllll"
BackColor = Color.Green
ForeColor = Color.Blue
Name = "Green"
Width = "149"
Height = "20"
Please note that for some properties, you will get a message that it's "read only". This means you cannot set it here in the class. Or can you?
H) Get into the form load event for your Form, and add this:
Dim LabelOne As New RainBowLabel( ) 'create an object from the class
Me.Controls.Add(LabelOne) 'add the object to the Form
I) run the program and oh crap.....! Now what? Well, we forgot to tell the program how big your Label is and where we want it. So its basically got NO SIZE AT ALL, and its located NOWHERE on the form! Here's what I did to fix that:
Add this to the code inside your class:
Location = New Point(200, 200)
Size = New Size(400, 55)
This says that our Class recognizes that these two things exist, but notice we don't specify how big our label will be here in the class, although we could have.
Add this code to your "form load" (see if you can reason out where it should go exactly)
LabelOne.Location = New Point(200, 200)
LabelOne.Size = New Size(400, 55)
Now let's run it again and see what we get!
OK, that's enough for this part. Now what I want you to do is to repeat the process, and make a class that will create some other custom control for your form. Use the same steps that I showed you, and also, see what additional things you can do with it.
Cheers,
Mr. L
Tuesday, December 16, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment