Page 1 of 1

Programming: The Very Basics (VB2005 Express) Part 2

Posted: Tue Mar 14, 2006 12:55 pm
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 ;)

Posted: Tue Mar 14, 2006 5:12 pm
by VoYdE
100/10

Nice

Posted: Tue Mar 21, 2006 2:56 am
by WastingBody
This will be great for the beginners.

Posted: Tue Mar 21, 2006 6:59 am
by Kurroda
i learned some stuff i didnt even no but i new most of it. good job

Posted: Tue May 09, 2006 10:53 am
by 0mfc0
Good job, hope you make another tut