Programming: The Very Basics (VB2005 Express) Part 2

This is where the Admins will put tutorials submitted by users.
Post Reply
Strang3r





Posts: 149
Joined: Sun Feb 05, 2006 9:36 am
Location: Cardboard Box

Programming: The Very Basics (VB2005 Express) Part 2

Post by Strang3r »

Well assuming youve read the 1st part of this i'll just continue on from where that left off...

This tutorial will give you more of an understanding of coding :)

@ Just a little preference, at the top of the properties box there are these 2 buttons Image all these do is change the way you view the items in the properties box; either catagorised or alphabetical.

Now lets get into coding :)

1) Open up the toolbox Image and drag onto your form a listbox and a textbox, in the listboxs properties there will be this
Image
click its little button on the right.

Another window will now appear, each line is another item, so add a load of text it before clicking OK
Image

2) Double-click the listbox and it will bring you to its default handle, _SelectedIndexChanged

First of all I will tell you how to make it so when you select a different item in the listbox, the textbox displays it. Add the following code:

Code: Select all

TextBox1.Text = ListBox1.SelectedItem
Press F5 to run your app to test it out :)

A nifty little feature the Visual team has added is a dropdown that appears when writing code

For instance, under the code i just gave you put:

Code: Select all

ListBox1
Now, put a dot after it, immediately a dropdown will appear listing all the things that you could put next, this is very useful if your in the unknown.
Image

Next to each item is a little picture, heres some and what they mean:

Image means that if you put a dot after it another dropdown would appear meaning you can put more coding after that.

Image means that if it is followed by a dot it will have continuous dropdowns coming up if u keep selecting the same option

Image means that it is a true/false statement so instead of being followed by a dot it should be followed by ' = true/false'

Image means that it must be followed by brackets (), when you put the 1st bracket a little message will pop up giving you information of what to put it the brackets, for example
Image

This is telling me to put in an integer (a whole number like 1, 2, 3, 4 etc) and the app will then check if the listbox item at that number is selected or not. Some code take 2 or more arguments like the messagebox

make sure your code once again just looks like this:

Code: Select all

TextBox1.Text = ListBox1.SelectedItem
Now go back to the designer and drag a button onto your form, double-click it 2 bring up the _Click handle, put in the following code:

Code: Select all

MsgBox
Now after that put a bracket, you will notice that a little message pops up again
Image

These are the optional things you can also add to the messagebox, 1st off you put in what text it will show:

Code: Select all

MsgBox("Put your text here"
Now after that put a comma; you are now onto what picture it shows, you will notice that a dropdown appears once again making life that bit easier, choose something from the dropdown then put another comma

The next part is the title so just put something like "Title"

And finish it off with an ending bracket, so it should look something like this:

Code: Select all

 MsgBox("Put your text here", MsgBoxStyle.Information, "Title")
Press F5 and click the button, rejoice as your msgbox flares into life :) :)

Declaring

Now its all fine and dandy to drag a listbox on, but it is also possible to create one using code, creation is simple and only requires 1 line of code, start a new project and add a button to the new form, put inits _Click handle:

Code: Select all

Dim NewList As New ListBox
Thats almost like saying 'make a new listbox and call it NewList'

Now, we will have to add a couple more things, for instance the size and location. To change the size simply add:

Code: Select all

 NewList.Size = New Size(268, 212)
To change the location we have to put how many points its down from the top and how many from the left:

Code: Select all

NewList.Top = 12
NewList.Left = 12
And finally to add it:

Code: Select all

 Me.Controls.Add(NewList)
'Me.' is the form and 'Controls' is all the things that are on the form

So your final code should look like:

Code: Select all

Dim NewList As New ListBox
NewList.Size = New Size(268, 212)
NewList.Top = 12
NewList.Left = 12
Me.Controls.Add(NewList)
Press F5 to test it out B)

An alternative to the code above is by using the 'with' command

Code: Select all

Dim NewList As New ListBox
With NewList
.Size = New Size(268, 212)
.Top = 12
.Left = 12
End With
Me.Controls.Add(NewList)
This will give you exactly the same result.

Well thats all for now, I may write up another tut, depends on how many replies i get ;)
VoYdE





Posts: 1474
Joined: Sun Aug 14, 2005 11:39 pm
Location: On Vacation From The Loads Of Bull*hit That Goes On Here
Contact:

Post by VoYdE »

100/10

Nice
Image
I made this 100% by myself, Everything from the background, to the location of the tags, Get yours http://customgamercards.com
User avatar
WastingBody





Posts: 195
Joined: Mon Sep 19, 2005 5:07 pm

Post by WastingBody »

This will be great for the beginners.
Image
Kurroda





Posts: 1737
Joined: Sat Sep 03, 2005 10:01 am
Contact:

Post by Kurroda »

i learned some stuff i didnt even no but i new most of it. good job
Image
User avatar
0mfc0





Posts: 218
Joined: Sun May 07, 2006 11:10 am
Location: Indiana

Post by 0mfc0 »

Good job, hope you make another tut
Image
Post Reply