Qbasicnews.com

Full Version: Visual Basic Tutorial bye ME!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Visual Basic, The Basics by:AlexBriner(Archangel)


::::::::::::::::::::::::Index::::::::::::::::::::::::::::

I. Creating the form
a. form tools

II. Putting code into forms.
a. were to put the code
b. code basics




======================================================================

Chapter I
-- CREATING THE FORM

Ok go to visual basic and clik on the icon. when visual basic loads there should b an open window that asks if u what kind of form u want to open. (if not go to file new and new project then follow instructions as given) ok you want the Standard EXE form (you will b using this in almost all projects so get use to it). ok clik on this clik open. there should b an average joe lookin form in the workspace (the program as u see it now is called the workspace). (if u dont see a form then go to the top bar and clik on the project explorer (it looks like two forms with some weird stuff around it.) ok now u have a form! you should experiment with things and figure some things out. now after exploring right clik on the form and go to properties u can change the name of the form, the name is used for what u will refer the form as in ur code and is usually the name u save the form as. so change the name if u like. another thing is if u look at the menu bar of the form is says form1 and i imagin u dont want that so do go to were it says caption and erase what should b "Form1" and replace with what u want. you should explore with the properties box some more before we go onto the next chapter.

-- Form tools

ok if u look to the left hand side there should b a tool box that consists of a picture box, buttons, labels etc. (if not go to the top bar and clik on what looks like a hammer and a wrench). we will be using these tools in out tutorial:
picture box,
label,
command button,
text box,
timer,
frame
shapes.

ok once u make a new form add everything in this list to it and place were u desire on the form. u can make bigger and smaller by goin to the hot spots (corners) and cliking and holding and draging to make bigger or smaller. go to paint and draw a picture aobut the size of the pictuer box and save it then right clik on the picture box and go to properties and find were it says picture: clik on that and there should b a small buton with three dots liek this: ... and clik on it now find were u saved the pictreu in and selcet it and press ok. now u have a grafix in ur form!!. yay. now for the label. go to label properties and u can do just as u did with the form and go to caption and change what the label says this is the primary tool for the label. now for the command button. u can not do much with that untill we get to the codes which is not yet. so so far just change the name and caption like u did with the label and form. we cant get to the timer yet. and the frame is easy the fram u pretty much use to frame objects try to place a frame around the label (if ur label falls behind the frame then keep the frame highlighted and go to format>order>bring to bak) and it will b ok again u can also use this vice versa. and for shapes u pretty much decide tha shape and draw it out.

Chapter II
Putting code into forms
ok well double clik on the form and u will come to a text box and it wil say:

Private Sub Form1_Load() (the form1 wil b watever ur forms name is)

End Sub (section ends)

all code is placed in certain sections if its for a button then its place in the buton section so u need to double clik on the buton to get to its section. if its for the form then it goes in the form load (only if u want that code to b activated wen the form loads other wise its in a different section.)
ok so just for kiks add a button to the form and dbl clik it, now ur in the section to put the code for the buton so wenever the buton is pressed the code wil b acivated. ok this is important if u dont want the butons code to go off wen the default clik is on the go to the buton code form and at the top right hand theres a drop down box and those are all the actions that u cna use for the buton so if u want wen the buton loses focus then u would clik lost focuz and that wil b how the commands will b executed but we wil not get int othat now. (also if u want somthin to happen wen they double clik the buton which might not be on the list jus tadd in front of clik add dbl so that its dbl clik for example:

Private Sub command1_DblClick()

End sub

were u need to add the code is between end sub and private sub.

ok now lets get to writin code basics.


-- Code Basics

(adding comments)
add this to anywere in code:
'
then after the ' place what u wana say for a comment
for example:
' this is my comment

Note: comments will b discluded from the code! and never make it more than one line! if so do this
' this is gona b longe than one line cuz i want it to but i run out thigs to say so alksdjfkjfjklsfklsd _
asdl;jkfasldkfjklasdjfk

there did u see wat i did? right before the line enede i added the _ and that tells VB to act like its still one line.
---------------------------------------------------------------------------------------------------------------------------------------------
(making the form red)

Private Sub Form_Load()
Form1.BackColor = vbRed 'makes form background red
End Sub
---------------------------------------------------------------------------------------------------------------------------------------------
(opening another form with the command buton)

Private Sub Form_Load()
Form2.Show 'makes form 2 appear
Form1.Hide ' makes form1 dissapear so it closes one form and opens another.
End Sub

---------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Form_Load()
Text1.Text = "hello this text will display in the text box named text1 in the form!"
End Sub

---------------------------------------------------------------------------------------------------------------------------------------------

one trick to make it seem like u did more code is instead of doing things in properties do it in code like this:

if i wanted to change the name of my label i would do this
Private Sub Label1_Click()
Label1.Name = "my name is this"
End Sub

so the syntax for doing it in code insted of properties is this
Private Sub (name of thing doing to)_Click()
(name of object).(what section of properties to change) = (what it would b)
End Sub

so u no how to change text now what about colors? well look at how i changed the form to red above its the same thing! u just enter wat u would wnat it to b and all colors are basic names with just vb in front of it

if u wanted to make it so wen u press a buton the form minimizes do this
Private Sub Command1_Click()
Form1.WindowState = 1 - Minimized
End Sub

to understand this more. do this go to form properties and down to were it says windows state and clik on the down buton and thers 3 things 0 - normal, 1 - minimized, 2 - maximized. so connect that with the code and u will understand

this is a sample program that we are going to make the things that we will b doing in this are putting code about the properties instead of using the properties menu and we will also b using the timer.
make a new form
add a timer to it (the icon that looks liek a timer obviously)
ok well first we want to make the background of the form be black so add this code to the Form load section:

Form1.BackColor = vbBlack

now we dont want a title bar so add this code to the form load right under the last code:

Form1.BorderStyle = 0 - none (sometimes this will still have there b a border so just to b safe go to form properties and go to border style and make it to none.

ok we also want the form to b maximized wen ran so add this code under tha last one:

Form1.WindowState = 2 - Maximized

ok now thats the end of the code for the form specs but we still need to add some code to the form load for the timer. so put this also under the form load.::
Timer1.Interval = 1500
Timer1.Enabled = True
Form1.Show

and for the timer code go back to the form design and double clik on the timer and add this code:
Me.Hide

this will make the timer invisible (although wen the programs run u will not see the timer anyway its good to add that code in this tutorial for you can add that to any object and it will make that object invisble
ok now run the project! (ok the top bar ther should b a triangle pointing to the right like a play button on a stereo)
if the program has no errors and will make ur entire screen black for about 1.5 seconds then you did this correctly! (if you want to make it last longer change the interval of the timer like this for example:
Timer1.Interval = 1500 u could make the 1500 3000 to make it last for 3 seconds the top ammound of time is about 99 seconds on a timer so it would b 99000.

then wen done with any project u ever work on to make it work as an executable go to file and make project1.exe to make it a running program! (note: name of program might change depending on name of project so it could say: make gogly.exe for all it cares)

the end
I like it, except for one thing: Could you get rid of some of the asteriks? It messed up the margin and made it hard to read Wink
k i fixed it! Smile
nice tut...
yeah, nice tut. but do you have a site? I think that it would be very helpful if you get one. for:

1)You can put as many asterisks as you want, and people wont
complain. :wink:

2)You have a place to put 'all' of these tutorials

3)last, you would be able to say, "hey, i have my own web site
that i can post projects and tutorials i make!".

now, go learn html Big Grin ......no, really, it takes like, 2 minutes to learn.
Come to my site (http://www.q-tech.de.vu) and submit your tutorial....you don't need webspace for it, but you can use every HTML command you know to make it look good.

Just register and goto your profile where you can add resources (tutorials, articles,...)
ya actually i do have my own site
its http://archangel7.proboards21.com