Visual Basic .NET Nightmare or Guide to upgrading a Guru.
Let's try to create a new control button with some unique attributes.
First setup your project and solution.
Add your first form and draw a button on it.
Now let's get back in the code. The first thing we need to get a hold of is the class code structure. You'll notice that so far everything has been written between the Public Class <name> and the End Class area. And I'm guessing you figured out that that was the nature of your class. But what else is interesting to note is that below end you can Begin a new class. Let's do that now. After you write your class statement it should automatically create a nice end one by itself.
Okay we want to start our class with a button class, so just like our forms designer we are going to inherit the button class for our new class.
Now we are going to need a timer, and a couple of variables. Then we can set them up in our Sub New().
You'll Notice a new feature for our VB7 IDE that we can now assign our variables in their declarations.
Private WithEvents MyTimer As New Timer()
Dim NewColor As ByteDim MaxSize As Long = 100
Dim MinSize As Long = 2
Public Sub New()
MyTimer.Interval = 100
|Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTimer.Tick If Not (NewColor + 1 = NewColor.MaxValue) Then NewColor = NewColor + 1 Else NewColor = NewColor.MinValue Me.BackColor = BackColor.FromArgb(CInt(NewColor.MaxValue - NewColor), CInt(NewColor), CInt(NewColor) / 2) Me.Height = Me.Height + 1 Me.Width = Me.Width - 2 If Me.Height > Me.MaxSize Then Me.Height = Me.MinSize If Me.Width < Me.MinSize Then Me.Width = Me.MaxSize Me.Refresh() End Sub|
Now let's take a closer look at our event code. In the beginning of the declaration you'll notice a few things that get passed to the sub when the event is fired. First we get ByVal sender as object. What this does is send the sub the object as a variable that called it. You can assign the sender object to a variable of your own, in this case the timer, and further use it in your code. At this point understanding ByVal is a must, because anything you do to that object will not effect the timer object, only the new temporary object you create from sender.
Next we have e as System.Event.Args. I think at some point we are going to find out this is the coolest thing since sliced bread, but maybe not right now. But let's comprehend it's name space. It's part of the System.Events collection and it's passing any Parameters (Arguments) that the System has for the particular event. It is of' course an Object and depending on the event it has various properties and methods that you can get to from inside your sub. In this sub it has some generic methods and one property, nothing special, yet.
The last part of our declaration is the event name by our key word Handles. Unlike VB 6 the name of our sub is completely arbitrary what's import is what it gets assigned to in this case our timers' Tick event. You can get all the dirt on Handles here.
Now let's take a look at our first line of code in the sub
|If Not (NewColor = NewColor.MaxValue) Then NewColor = NewColor + 1 Else NewColor = NewColor.MinValue|
Our NewColor Variable is first to be checked against it's max value.
What the heck is going on here? Our Variable NewColor has properties? You betcha. And some methods to boot. What we normally would get to from the VBA object to manipulate variables now is a part of the Variable's Object. Once declared it creates a new object who's default value the Variable assigned to it according to it's type. So for our Byte it's value property, which is implied when typing just the name, is it's data.
This introduces us to one of our most needed variable methods, the .ToString method which returns a string representation of the variable that's calling it. There's also it's opposite for this particular type it's called .Parse, which when given a string representation of the data you want to be converted will convert it to the variable type that called it. Keep in mind this returns a Byte and doesn't automatically reassign it to your variable. So you would need to set your variable like so NewColor=Newcolor.Parse(200)
The rest is pretty clear, if it's equal to the .MaxValue it's going to assign it to the .MinValue and continue to count in a loop each time it's run in the tick event.
Next line looks like this.
|Me.BackColor = BackColor.FromArgb(CInt(NewColor.MaxValue - NewColor), CInt(NewColor), CInt(NewColor) / 2)|
First let's keep in mind that within our Insane Class the Me keyword now refers to our insane textbox and not the form. So we are changing the .Backcolor for our insane button.
Now our .Backcolor property of our class can be referred to without the Me keyword because it's a member of our class, so you see both ways in this statement. Once again you'll notice it's not a mere property, it as well is an object belonging to the button object. It's base value is of' course the Data signifying the color. There are a few different ways to set the data with this property. You could pick one of the preset system colors that show as properties of .Backcolor. In which case you might have a statement like this Backcolor=Backcolor.Aqua. This would get the job done, but we really want to mess with the color so we are going to use the .Argb Method of the .Backcolor object.
We are going to generate 3 different numbers based on our NewColor variable. one by subtraction, one as the base and one by division. The method is taking these 3 values and converting them to integers to be passed to the method. The method takes integers but for R,G, and B only a Byte range is valid from 0-255. This is why we are tracking it as a byte before using CInt() function to pass it to the .Argb method. This method is similar to the rgb() function in VB 6 which gives us a value to pass to the .Backcolor property. For more info on this click here
= Me.Height +
Me.Width = Me.Width - 2
If Me.Height > Me.MaxSize Then Me.Height = Me.MinSize
If Me.Width < Me.MinSize Then Me.Width = Me.MaxSize
Now we are checking Height and width against our preset MaxSize variable and resizing the the Insane button accordingly every time the Tick event is fired.
Then we call refresh to force an update to our buttons Graphics. Without this it may not redraw until the system says to and we may miss some of the new coloring.
Now let's see what kind of monster we just created. Go ahead and collapse the whole Insane Class area of code.
Now let's go into our designer code. Let's change 2 lines of code.
Me.Button1 = New System.Windows.Forms.Button()
Change the above code to point to our insane class instead of the button class.
Me.Button1 = New Insane
Now do a build. Then switch to the forms designer view. Kind of crazy isn't it?
As for reusable code this probably wouldn't market very well, but this gives an even better view on how classes create objects and become our applications.