Forum Authority

Full Version: An Introduction to Visual Basic - For Beginners
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Welcome to an introduction to 

[Image: vb.png]



What I will cover in this thread 

Making a Project 
UI Items (The Visual Side of Things) 
Events 
Some Basic Functions


A Brief Overview 

For me Vb (Visual Basic.Net) is brilliant for a starter coding language!! Its super easy to use and supports a wide range of features and 'add-ons' (.dll's). Which Basically means the outcomes are virtually limitless. 

VB is super easy to learn and doesn't take that long (compared to other languages) which is why for me its great to start with. What really makes Vb so great is that is has it's own visual side of things, this allows you to create your application's UI (User Interface) with little to no effort. Other languages do not have this and is all text based, which can be super tricky to jump right into!! 

If you don't already know Vb is designed to make Executable files (.exe), Executables are programs that give a computer a list of commands to execute. Hence the name 'Executeables'. 

However, Visual Studio does allow you to make other types of applications too!! However, I'm not going to talk about that in this thread.



Creating a Project 

Okay, so before you can do anything at all you need to load up Vb & Start a fresh new project. This is easily done and is as simple as creating a new text document. So here's how you do it!! 

Firstly, your gonna need to open Visual Studio. Then click select (located left side near top) 

[Image: 64ea6b7757956a010393a31524cb0f37.png]

When that's selected you will be prompted by the following 

[spoiler][Image: f6fb3ebe5aa2d8d92c2bcac1e6be9673.png]
 Now Make sure 'Windows Form Application' is selected. Now give your project a name and when that'd done select 'OK' & Your project will be created. How easy was that?
Now Make sure 'Windows Form Application' is selected. Now give your project a name and when that'd done select 'OK' & Your project will be created. How easy was that?[/spoiler]



UI Items (Visuals) 

Let's get down to business, First thing I'm gonna run you by is UI Items (Visual Items). As I mentioned Vb has it's own visual side of things, which is why its so good. When your making an application you make the Front-End (Visual UI) and then once your layout, design and other features are all set and ready you will continue on to add the code to these items. Now, without any code Items (Such as buttons) will do absolutely nothing as its only visual! What makes the button actually serve a purpose and do something is the Back-End Code side of things (We will cover this later on) as it gives the computer running the .exe instructions to carry out. 

Adding in items to your soon to be application is very easy and is self explanatory when using Vb, but just in-case I'll run you over it! Once you have your very first project created you will most likely have a window displaying a blank form (A form is a window of which the application will show). You may be thinking, wow this application is great I can do just about nothing!! But wait, soon enough it will do something (whatever YOU Want it to) 

So, when you've made your project it should look something like this 

[spoiler][Image: e7ef41d0f655fc1347f38d75713ca9fe.png]
[/spoiler]

If it looks slightly different that's fine!! Now lets get into things. Firstly, anything you put into Visual Studio (Including forms) have Properties which are unique to that item. These CAN be changed, as you see the applications window (the form) Has the text "Form 1", which nobody wants!! If you select an item you want to edit the bottom right toolbar will show all the properties for that item/object. To change the "Form 1" Text, click the form itself and now find the property 'Text'. Click the text area and type what you want it to say! Wow, amazing right? 

Now lets get down to business and add your first item / object into your application, to do this hover over the tab named 'Toolbox'. This tab will now display many items that can be added into your application!! For this I'm going to add a simple Button. When I've added this I can re-size it and do anything I want with it!! I'll show you what it look's like now I've added the button (I also resized the form) 

[Image: 2f184ed9361941f5e11b96e275a6977e.png]

Ahh yes, beautiful isn't it? This shows why Vb is so good. Within a few clicks and drags I have a button on my screen which required no code to show!! Other languages require you to manually add buttons and other items with code such as (HTML) 


Code:
[align=center]<html> [/align]
[align=center]<button type="button">Click Here!</button>[/align]



The above doesn't even state the location, which can be a real nightmare to add (as of working out locations etc). Now do you see the true power of Vb?! Yeah you should do. 

Well, this basically sums up adding in items. It's not really rocket science, its literally dragging and dropping and maybe clicking and editing a few properties... 
If you want to get more familiar with item names and what they look like take a little look here (slighly outdated as its for VS 2008): dummies.com/programming/visual-ba...c-toolbox/



Events & Event Handlers 

Now let's tackle 'events'. These are still basic but may seem really complicated at first, Coding is like anything else that you have to learn. Scary and hard at first but super easy after a short while!! So are you ready for this? Good. What are 'Events' you may be wondering. A UI Item needs something to declare the action performed, so if a button you want a button to execute a command when clicked then the button will need a Button_click handler event (Handler event = Handling of the event which in this case is a click). Got that?! 

What do I need events for? To Put a long story short, everything. If you want an item to execute a command when action 'x' is performed on that item then you need event handlers. I have good news now, You don't have to worry about creating the code for them!! Why? To create the event handler code for your project simply double click the item you wanna add the code to. 

You may be a little confused now, so an event handler handles a specific action yeah? Now, you need these EV (Event Handlers) to add code into. All that an EV is, is a line of code which creates the event, meaning that code can then be added and allowed to be executed. If I wanna add code to the button I made before I will simply double click it and it will be created and then the Back-End side of things will show!! Like this 


[spoiler][Image: a02acac5347adc759357d18b63414313.png]

[/spoiler]

Hopefully you now have a brief understanding of what an Event Handler is!! If not, then you will do very soon. Now should we look at adding in different types of events for different object / items? This is super simple and again does not require any sort of manual coding, just a click of a button. The default event selected when you double click a button on your form is the event "_click" (yes it is what it sounds like). What if i wanted to add code for when the button is double clicked? Well, at the top of the window you will see this a combo box which displays the items name (in this case Button1) followed by the event type in another box (In this case 'Click'). Now if we want to change the event click the second box and it will show different types of event, for this I'm going to select 'Double Clicked' and when I do so it will create more code for the Button1 Double Click Event Handler!! As shown below 

[spoiler][Image: 2140187e781f1a70de3e89a043462327.png][/spoiler]


Yeah, its really that simple to change!! I think that's all to cover for Events & Event Handler's. Now lets jump straight into some basic code. This is a lot more advanced than the rest of what I have covered, but we will take it nice and slow and use very simple commands!!



Basic Functions (Code)


Okay, so now I'll run you over some really basic functions. To start off we will not use any Item specific functions, just nice and easy ones!! A brilliant example of this is a Message box which displays the text you want it too. Like all things in Vb there are multiple ways to do so, but I'm only going to show you the easiest and simplest method of doing so! Now, say we want our button (from earlier) to display a message box when clicked (Once). We will need to use our Button1 Click Event Handler!! Which sounds a lot harder and scarier than it actually is... 

So, to do this make sure your on the Back-End side of things in Visual Studio. Now Underneath the text



Code:
[align=center]Private Sub Button1_Click[/align]
[align=center][/align]
 
(that is our event handler but I haven't typed it all) we will start to code and tell the computer what to do!! To make a message box display some text were going to use the following code (Which I will break down for you)


Code:
 Msgbox("Hello World)




The above would show a Message Box displaying the text 'Hello World', with no style settings or title!! I'll teach you how to add these In a minute. So lets take a look at the code. Firstly 'Msgbox' calls the Message Box function, meaning it tells the computer we want to display a Message Box. Now, the ()'s are the parameter's for the Message Box, in basic terms... The text to display, The title to show and what style to use (These are a few parameter examples). So anything inside the ()'s is specific information used to generate the Message Box. Finally, we have the set of " ". These are used how they are in English language, to show a quote. The quote in this situation is the text to display, even though we have not stated anything like msgbox(text to display="hi"). This is because the default parameter for a Message Box is the text, meaning we don't have to declare it. 

Now, lets use some more parameters. We will make the Message Box display the same text but with a style and a custom title to it!! To do so we will use the following code 



Code:
Msgbox("Hello World", MsgBox.Style.Information, Title:="Parameters")




The above will now create a Message Box that will display the same text as before however with a title of 'Parameters' and the Information Pre-set style (Displays an Information symbol). Should we take a look at the code and break it down again? Yeahhh. 

Okay so, we will cover only the code after the "Hello World" Text. First of all, straight after the " we have a comma (,). What does this do? What this little comma does is notify that we are done using the parameter that we have called for (or the default) and that we are now ready to use a new one. Now, we have 'MsgBox.Style.Information' We already know that 'MsgBox' is declaring the object, so the 'Style' after it is declaring a whole new object yes? No. As we are already using that items Parameter's we do not have to use MsgBox.Style, We can simply call it without having to use Style as a new Parameter... So 'MsgBoxStyle' simply means that we would like to set a new value for this Parameter. This is then followed by '.Information' which is a new parameter for The MsgBoxStyle, as it is part of the Style set and not the MsgBox itself!! '.Information' simply tells the computer that we wish to use the 'Information' Style Pre-Set for our Message box!! Now, you should be able to understand the whole concept of 'Title:="... If not, As the title of the Message Box can only be one item and no other settings like colour etc we do not have to declare anything and its as simple as using "'s!! 

Now, when you've got your code added you wanna test it right? To do, either click 'F5' or at the top toolbar click the little green play icon (Looks similar to the old Youtube logo) and this will now bring up your application which you can test your self!! This is called 'Debugging' Your application, to stop debugging when your done testing go back to where you started debugging and click the stop icon (or press F5 when in Visual Studio) Here's my code in action... 

[spoiler][Image: 634a97fa021dbdbe24de193580552b6c.png][/spoiler]


Should we try adding text into a textbox? Yeah?! Okay, so first of all we need to need to add the textbox to our form. Now, you may think the next step is to crate an event handler... Wrong! We don't need an event handler (unless we wanna perform an action to the textbox and make it change text) as we are going to use our button to change the text!! 

So under Button1's Click Event Handler were going to add the code 



Code:
 Textbox1.text = "forumauthority :)"




What this code tells the computer to do is, set the text (property) of the Textbox to the text inside the "" 's. The text can be whatever you desire!! Now, if you think you have the hang of it you should understand that very simple line of text. I'm not going to go over it as that's for you to work out on your own  + Its 2:35AM and I have college at 9AM   



Thanks for taking a look at this! Also if you have Vb Knowledge & Have spotted anything that needs changing / adding just let me know down below!! 

Note: If you need any help with VB.NET Please don't hesitate to contact me Via PM, Im always free to help you 
Thanks for posting this, reminds me of my younger days when messing about with VB.