![]() |
|
[.NET] [Tutorial] Visual Basic for Beginners [Tutorial] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] [Tutorial] Visual Basic for Beginners [Tutorial] (/showthread.php?tid=1112911) |
[Tutorial] Visual Basic for Beginners [Tutorial] - Androcir - Mar 8, 2011 Welcome to my VB Tutorial for Beginners. Let's get started Things you will need: - Visual Basic 2008/2010 - You'll have to atleast know how to work with VB First Script The very first script we'll be making is the typical "hello world". 1. Start VB 2. Create a new Project 3. Then in your windows form add a Button, and name it Start. 4. Then double click the button, and in the source code type in, the following: Code MessageBox.Show("Hello World")In the Button source code there should be this: Code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickThe private sub Button1, introduces the program to the Buttons function, which in this case is displaying a message box saying "Hello World". This is basic coding, thats why it is the first script that we're writting. Variables A variable is a letter,number or sign that has a value. This value can be a number, word, string, etc. As a variable it means its value can be changed into other value. 1. Start VB 2. Create a new Project 3. Then in your windows form add a Button, and name it Start. 4. Then double click the button, and in the source code type in, the following: Code Dim intNumber As Integer5. Run the program and see what happens. There should appear, after you click the button, a little window displaying the message "intNumber + 1 = 28" In the first line of code we declared an integer (Variable) to the program which is named as "intNumber" and has the value of 27. After that we informed the program that, that same variable is equals to "it's value + 1". We made a message box that will display the string "intNumber + 1 =" and after that we ordered the message box to display the "intNumber" as a string with ".ToString". The "Variables" is considered to be the title of the message box. As i made with the plus sign you can make with the other signs, like this: Times Code Dim intNumber As IntegerDivision Code Dim intNumber As IntegerMinus Code Dim intNumber As IntegerList Boxes A list box, is a box which works as a List of Items. 1. Start VB 2. Create a new Project 3. Then in your windows form add a Button, and name it Add. 4. Create a TextBox 5.Create a List box. Spoiler (Click to View) 6. Double click the button, and type this in the source code: Code ListBox1.Items.Add(TextBox1.Text)7. Run the program, type anything in the text box, and click the button "add". There should appear in the list box the string that you typed in the textbox. As for the code it is quite simple, basically you're telling the program to add to the listbox everything that is written in textbox. Radio Buttons A radio button can become really usefull when you want the user to make a choice. And for this we're going to make an intro to the if statement. 1. Start VB 2. Create a new Project 3. Then in your windows form add a Button, and name it Check. 4. Add a groupBox And name it "Your Age" 5. Add 2 Radio Buttons and name the first one "-18" and the second one "+18" Spoiler (Click to View) Double click the Button, and type this in the source code: Code If RadioButton1.Checked = True Then6. Run the program. The if statement is really usefull because it introduces a possibility to the program making it easier to run choices such as radio buttons. What we told the program to do: 1st. If the radiobutton1 is checked then messagebox will appear saing that you're under 18. 2nd. If the second radio button is checked, then a messagebox will appear saying that you're over 18. Text to Speech In this part of the tutorial i wont be teaching much of programming, instead i though of making something cool, like making a program that will speak whatever you type in a textbox. Create a Project 1. Add a TextBox and a Button (name it "Spell it") Here's an example: Spoiler (Click to View) 2. Double click the button and type: Code Dim SAPI3. Run it. Write something in the textBox and click the button. Listen to the computer speaking what you just wrote down. This code is really simple. Basically you created a mini vbs file that is recognized as voice producer. And in the third line of code you're telling that voice producer to produce what is written in the textbox. The For Loop This part of the tutorial brings us to an intermidiate programming skills, it is easy to understand, but sometimes it gets a bit complicated to explain. Start by the usual proceder. 1. Add a button and name it "loop" 2. Double click it and write this code in: Code Dim i As Integer3. run the program. Basically what this does is constantly show a message box displaying the integer "i" with its different values. But when its value reachs 10 the loop will stoped, as commanded in the "For i = 0 to 10". Do while The do while statement is simple as making something while something is "whatever". It really is just like that. 1.Add a button and name it "Do while" 2. Double click it and write this code in. Code Dim num1 As Integer = 13. Run it. What this does is Messagebox the value of "num1" while its value is under 10. So you will see many messageboxes appearing with different numbers from 0 to 10. Do Until The Do until statement is really similar to the do while statement. With just one difference, the do until statement runs the function until it reachs the limit you typed in. 1. Add a button, and name it "Do until" 2. Double click it and write this code in Code Dim a As Integer3.Run it What you just made is, introduce the integer "a" and messagebox "a + 1" until "a" becomes greater than 10. After that the loop will end. Subs A Sub is a way for you to type a code and execute it later. 1. Create a Project 2. Add a Button and call it "Sub" 3. Double click the button. 4. Now under "Public Class Form1" type in the following: Code Sub message()5. Now , in the button Private Sub just type in: Code Call message()You should get something like this code: Spoiler (Click to View) 6. Debug the project, and click the button. Now, there should appear the messageBox saying "Used the Sub", although it wasn't typed in the button Private sub, this happens because you declared the code before and called it after to be executed in the Button function. Functions A function is a piece of code that will allow you to get to a specific part of the program and change its contents. 1. Create a Project 2. Add a Button and name it "Function" 3. Add a Label. 4. Now in the Public Class Form1 type this code: Code Dim labeltext As String = "Used the Function"5. Bellow that you'll have to create a Private Function, where you will return the integer "labeltext". Code Private Function ChangeLabel1Text()6. Code the Button. Code Label1.Text = ChangeLabel1Text()You should have a code similar to this: Spoiler (Click to View) 7. Run it, and by clicking the Button you should see the Label changing to "Used the Function". Explanation: First of all you declared an integer named "labeltext" as string that will display "Used the Function". Than you created a Private Function that will Return the Integer "labeltext" to the program so it can be used by the button. And finnally the button will change the "Label1.Text" content, which is "Label1" to "Used the Function". Advanced MessageBox In this part of the tutorial, i'll be explaining how to make an Advanced MessageBox, by changing its title, icon and Buttons. 1. After creating a Project, add a Button and name it "Start" 2. Double click and in it type the following: Code If MessageBox.Show("blablabla", "Acvanced", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then3.I used the 'if' statement in this code, to display a new messagebox depending on each button the user clicks (Yes or No). 4. Run the program. How the code works -if the messagebox, which displays the text "blablabla", has the title "Advanced", has the buttons "yes" an "no", and has the icon of a question ('?'), has its button "yes" clicked, then the program will display another messagebox saying "You clicked yes", else the program will display another messagebox saying "you clicked no". Here's is the function for each bit of code: Code MessageBox.Show("blablabla", "Acvanced",Code MessageBoxButtons.YesNo, MessageBoxIcon.Question)Conditional Operators A Conditional Operator is nother the less then the: 'equal to' ('=') 'greater than' ('>') 'lesser than' ('<') 'not equal to' ('<>') Curiosity: The "<>" Operator can be used in Log In programs. 1. Create a Button and name it "check" 2. Create a textBox. 3. Double click the Button and type in this code: Code If TextBox1.Text <> "123456" ThenWhat this code does is, check if the password that you entered in the textbox is the correct one or not. In this case if you type something other than "123456" the program will display the messagbox saying that you typed the wrong password, and if you type the correct password, it will display a messagebox saying so. Timer A timer is an option that comes with Visual Basic that allows you to, well.. make a timer. And you can do it this way: 1. Create a project and add 2 Buttons and 1 Textbox. 2. Name Button1 "Start" and Button2 "Stop" 3. Change the textbox text to "1" 4. Add a Timer. 5. Now double click the "start" Button and type in this code: Code Timer1.Start()6. Go back to your form and double click the "stop" button, and type in this code: Code Timer1.Stop()7.Go back to your Form, and this time double click the timer icon which is located in the bottom of the screen. And type in this code: Code TextBox1.Text += 18. Go back to the form, and in the timer properties change the interval To whatever you want ( 1000 = 1 second ) this will determinate the time interval which with it the timer will execute the code over and over again. Try and set it to 1000. 9. Finally run the program and click "start". I am not going to explain this code, because it is easy to understand, and if you followed the tutorial properly you should be able to understand this one by yourself. Make a Web Browser What i'm going to teach you now is how to Create a Web Browser. Let's get Started. 1. Create a Project, Make your Form a BIG one. 2. Add 5 Buttons, a TextBox and of course a Web Browser. 3. Make it like this: Button1 -> "Visit URL" Button2 -> "Back" Button3 -> "Forward" Button4 -> "Refresh" Button5 -> "Homepage" Example: Spoiler (Click to View) Now do exactly as this: Button1 "Visit URL" Code WebBrowser1.Navigate(TextBox1.Text)Button2 "Back" Code WebBrowser1.GoBack()Button3 "Forward" Code WebBrowser1.GoForward()Button4 "Refresh" Code WebBrowser1.Refresh()Button5 "Homepage" Code WebBrowser1.Navigate("YourHomepagehere")4. Now in the webrowser source code type the following: Code TextBox1.Text = WebBrowser1.Url.ToString()This code will make the textbox change its content acording to the web page your visiting. Run the program and test it. Here's an example: Spoiler (Click to View) And that is all for the tutorial, i hope i helped you in some way, and please reply to this thread asking your questions, i'll do my best and try to answer them. Special Thanks TeachMeComputer Youtube Channel * - Mr.Feelgood - Mar 8, 2011 haha, saw all this today on his channel:P Great Tutorial m8:D Will help many beginners I think:P * - Androcir - Mar 8, 2011 (Mar 8, 2011 07:04 AM)XmGX Wrote: haha, saw all this today on his channel:P Great Tutorial m8:D Will help many beginners I think:P It is a Great channel! I learned a lot with that, but i made some changes to this tutorial, wouldn't want to be making a copy. And thanks for the coment. * - iGnite - Mar 8, 2011 Your tutorial is put together very well. Thanks for the contribution, and keep up the hard work. * - Androcir - Mar 8, 2011 (Mar 8, 2011 12:16 PM)iGnite Wrote: Your tutorial is put together very well. Thanks for the contribution, and keep up the hard work. You came and comented it :D Thanks for the comment iGinite! * - BTat - Mar 9, 2011 Read this almost word for word in a VB.Net book I bought in the shop a couple of years ago! * - BaussHacker - Mar 9, 2011 You are doing some strange things. Examples on your mistakes. Mistake: a = a + 2 Correct: a += 2 Mistake: a = a * 2 Correct: a *= 2 I hope you will use that in future. Other than that is a nice tutorial. Goodluck learning. * - Androcir - Mar 9, 2011 (Mar 9, 2011 07:30 AM)BaussHacker Wrote: You are doing some strange things. Once again, thanks =) *too short * - BaussHacker - Mar 9, 2011 No problem. If you need help with other things or if you got questions, just let me know :) * - SuckMyDong - Mar 11, 2011 Very good tut! Keep up the good work bro ! Just followed from step 1 to end and succeed without any errors Great tut for beginners :) 5 Stars ! * - BaussHacker - Mar 11, 2011 Oh I saw one thing more mate. Remember there is a different on name and text in form properties. Name is the name of the control and text is the text it show. * - Androcir - Mar 11, 2011 (Mar 11, 2011 03:34 AM)BaussHacker Wrote: Oh I saw one thing more mate.Yeah i notised that a yesterday, although i coldn't change it. But thanks for your attention! =D * - Fake. - Mar 11, 2011 (Mar 9, 2011 07:30 AM)BaussHacker Wrote: You are doing some strange things. Wrong, no. Longhand, yes. * - Androcir - Mar 11, 2011 thought there's something i don't get. I type in for example: Code a = a + 1This is to change the value of a to a + 1. i don't get the: Code a+=1thingy =/ Care to explain? Please =) thanks in advance * - Shopify - Mar 29, 2011 Error 1 'WebBrowser1' is not declared. It may be inaccessible due to its protection level. i get errors like this on all my buttons :S? |