Technique Tutorial

Discussion in 'Programming & Scripting' started by Michael Allison, Nov 10, 2008 with 2 replies and 169 views.

  1. Michael Allison

    Michael Allison Enthusiast

    Messages:
    232
    Ratings:
    13
    [​IMG]

    Most people who started programming with an object-oriented language would think the above form does nothing. However, there are various instances in which a Visual Basic programmer (or coder of a similar language) will need to change and create objects in code, rather than design them graphically in the compiler. The following code is an example of this.

    Code:
    Public Class frmMain
    Dim tm As New Timer
    Dim i As Integer
    
    Private Sub loadinmahformnow(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Height = 120
    Me.Width = 120
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    Me.Text = "Look Mom, No Toolbox!"
    Dim t As New TextBox
    Dim pt As New Point(0, 0)
    t.Location = pt
    t.Height = Me.Height
    t.Width = Me.Width
    t.Multiline = True
    t.Parent = Me
    t.Show()
    tm.Interval = 950
    tm.Start()
    AddHandler tm.Tick, AddressOf gabeksucks
    End Sub
    Private Sub gabeksucks(ByVal sender As Object, ByVal e As EventArgs)
    i = i + 1
    If i = 5 Then Me.Close()
    End Sub
    End Class
    I only chose about three things to cover in this tutorial, but I think they're essential skills to learn for advanced Visual Basic coding. First we'll look at how to create a brand new object from code. We'll be focusing on the following section of the above code.

    Code:
            Dim t As New TextBox
    Dim pt As New Point(0, 0)
    t.Location = pt
    t.Height = Me.Height
    t.Width = Me.Width
    t.Multiline = True
    t.Parent = Me
    t.Show()
    All of the most important aspects of an object are encompassed in this code. These are Location, Height, Width, and Parent. Once these are defined, you can activate your new object with Show, just like in the above segment of code. If even one of the items listed above isn't defined, your object will not be as you intended! Parent simply means where in the form the object is, or more specifically what it's in. A Parent can be a form, a panel, and so on. You should memorize how to declare a point and what it's used for, so you don't try using an integer or string for the Location of an object by mistake.

    The next thing we'll check out is how to create a timer from code. The following segments of code are important to this part of the tutorial.

    Code:
        Dim tm As New Timer
    Dim i As Integer
    Code:
            tm.Interval = 950
    tm.Start()
    AddHandler tm.Tick, AddressOf gabeksucks
    Code:
        Private Sub gabeksucks(ByVal sender As Object, ByVal e As EventArgs)
    i = i + 1
    If i = 5 Then Me.Close()
    End Sub
    First create a new Timer, as shown above. Also, for later, create an Integer. Next set any specific traits of the timer. I only messed with the Interval setting, because I wanted each tick to be in perfect sync with the blinking of the cursor. Yeah, I'm weird like that. Anyway, then comes the tricky part. Leave the sub you've been creating your objects in to create a new, blank sub. Use the default values and such, and then use your previously created integer to set a limit for the timer. In my example, I made it close the main form after five ticks, because I removed it's border on startup. As long as you have an alternative way to close your form, it doesn't matter what you do with this sub. It's just to show you how to make that timer you made earlier actually do something!

    On that note, after you've filled out that new sub, go back to the bottom of the old one. Right after you've created your timer, and right before you've started it, follow suit to what I did using AddHandler. AddHandler is followed by a space, the event you're using as a handler, another space, the text AddressOf, and then finally the name of the sub you wish to add the handler to. Pretty straight forward, if you look at the example code provided.

    The last and least important thing is what's below.

    Code:
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    When using multiple forms, it's sometimes useful to vary the FormBorderStyle you use depending on the situation. I pointed out this one in particular, because you have to realize the user has no way to close a form using this specific border style from the form itself, unless an alternative to the usual is provided for them.


    That's pretty much all. Since the code was fairly self-explanatory, I didn't really bother with long, drawn out explanations of some things. If you didn't understand something, tell me, and I'll see what I can do to help you with it!
     
  2. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Really, the title of this thread should be "How to Programatically Add and Manipulate Controls" lol.

    Good work though, someone out there will probably learn a lot from this.
     
  3. OP
    Michael Allison

    Michael Allison Enthusiast

    Messages:
    232
    Ratings:
    13
    Yeah, it was late where I posted this from, though, so I'm surprised I could spew out as many words as I did. :tongue: And thanks! :smile: