+ Reply to Thread
Results 1 to 3 of 3

Thread: VB.NET from beginner to advanced programmer Part 2 - Objects and Events

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    VB.NET from beginner to advanced programmer Part 2 - Objects and Events

    Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.


    VB.NET from beginner to advanced programmer
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. Compiling Directives



    This part will continue from the last one so we will continue on the project we had in Part number 1, in this Part we will add a few objects to the form and also add some simple code to it. So after you have loaded the project we can start.




    To the left you can find something called Toolbox. Here you can find all default objects(it's also possible to add more of them). To add one to your form just click and drag from the list to the form. You can modify this object a bit by just using the mouse but you have a lot more things you can modify in the properties menu to the right.


    Now add one button, one label and one textbox and change these things in the properties window:


    The button:

    Code:
    Name="cmdOK"
    Text="OK"

    The label:

    Code:
    Name="labName"
    Text="Please enter your name and press OK"

    The textbox:

    Code:
    Name="txtName"


    The thing we just did was to rename them as we wanted and also to change the text of the button and label, after placing them where you want the form could now look like this if you did everything correct:


    VB.NET from beginner to advanced programmer Part 2 - Objects and Events-first-program-form.png



    Now we're going to add an event. Events are a sort of sub(a block of code, but we comes to that later on in the series) which will be called(the code in the block will run once) when something is happened to the object. There's 3 ways to add an event.

    The first way is to add the default event, it's just by double-clicking on the object in the design view. The default event for a button is when it's clicked and for a textbox it's when the text is changed etc. When doing this you will automatically be taken to the code view. Another way to open the code view is to right click on the form in either the design view or the solution explorer and then select view code.

    If we are in the code view we can add which event we want by just selecting the object and the event we want at the top of the code view. Then the event block is added automatically.

    VB.NET from beginner to advanced programmer Part 2 - Objects and Events-events.png


    The third way is to write them on your own, this is not recommended for new programmers.




    Now we want to add a click event block for the button. Since the click event is the default event for buttons you can just double-click on the button.


    After doing this we will have this code:


    Code:
    Public Class frmMain
    
        Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
    
        End Sub
    
    End Class

    This thing about a class:


    Code:
    Public Class frmMain
    
    End Class
    Is basically all code that exists in the form. What it's actually are and how to create and use your own classes we'll talk about later on. The only thing you actually need to know now is that we should put all our code in it.







    What we're actually interested in is the newly created event block:


    Code:
        Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
    
        End Sub
    All code we put in this will be run each time the user presses the button.


    Add this code inside the block:

    Code:
            'A messagebox will be showed with the text from the textbox
            MessageBox.Show(txtName.Text)

    The first line is just a comment, comments are created by adding a single quote at the beginning of it. You should use comments a lot, it's good if someone else will look at your code or if you look back on your code months later and have forgot what everything does.


    In the second line you can see:

    Code:
    txtName.Text
    in this way we access the text property of the texbox txtName. Then we are using the text as the text of a messagebox. So the second line will show a messagebox with the same text as the textbox.







    Save your project now.


    Then it's time to test the program, press F5 to start the debugging. Test to write different things and then press Ok. If I enter "Vswe" in the textbox and then press Ok it will look like this:


    VB.NET from beginner to advanced programmer Part 2 - Objects and Events-messagebox.png


    You have now created your first program, we'll continue in part 3.
    Last edited by Vswe; 11-02-2009 at 01:51 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 2 - Objects and Events

    Nice and gentle intro into code, good work. +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: VB.NET from beginner to advanced programmer Part 2 - Objects and Events

    Nicely done. +rep

    I had a chance to see a coder working on VB2008 last week. It's not a bad language.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 05:37 PM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  4. VB.NET from beginner to advanced programmer Part 10 - Arrays
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-02-2009, 05:45 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts