Jump to content

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

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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



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:

Name="cmdOK"
Text="OK"


The label:

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


The textbox:

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:


[ATTACH]2259[/ATTACH]



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.

[ATTACH]2261[/ATTACH]


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:


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:


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:


    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:

        '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:

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:


[ATTACH]2260[/ATTACH]


You have now created your first program, we'll continue in part 3.

Attached Files


Edited by Vswe, 02 November 2009 - 01:51 AM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Nice and gentle intro into code, good work. +rep

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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