Introduction: Creating Your First Program in Visual Basic

Picture of Creating Your First Program in Visual Basic

This instructable will show you how to program Microsoft Visual Basic 2005 Express Edition. The example that you will create today is a simple image viewer.

If you like this instructable please push the + button at the top of the instructable. Thanks.

Also, I think I will be like half the other instructables out there and say that this is my first instructable and please don't be to harsh.

EDIT: Once you have completed this instructable, continue learning VB with my second Visual Basic Instructable: Creating a Program in Visual Basic: Web Browser

Step 1: Download Visual Basic

Picture of Download Visual Basic

You can download visual basic 2008 from microsoft but this instructable is specificly for VB 2005 wich you can download from freeware files

EDIT: It is now recommended you VB 2008, as I will use it for any future tutorials. Link

Please note: you will still need to be connected to the internet during the install.

Step 2: Create Your Project.

Picture of Create Your Project.

Click File->New Project. Select "Windows Application". Give your project a name.

Step 3: Add Controls

Picture of Add Controls

From the tools box, drag a picture box onto your form, drag a button onto your form and drag an open file dialog onto your form.

Step 4: Edit Control Properties

Picture of Edit Control Properties
Now it's time to edit the properties of the controls. To edit properties, click on the object and change the values in the properties window.

Form Properties
  • Text: Picture Viewer
  • Form Border Style: Fixed Tool Window

Picture Box
  • Background Image Layout: Zoom

Button
  • Text: Select Image

Step 5: Add Code

Picture of Add Code
Double click on the button and replace all the text in the code window with the following:

Begin Code
Public Class Form1    Private pic As Bitmap    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        OpenFileDialog1.FileName = "Select File"        OpenFileDialog1.ShowDialog()        pic = New Bitmap(OpenFileDialog1.FileName)        PictureBox1.BackgroundImage = pic    End SubEnd Class
End Code

WHAT THE CODE DOES

Public Class Form1 - Defines the form as Public
Private pic As Bitmap - Defines pic As a private bitmap

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click - Defines When The Events Should Occur
OpenFileDialog1.FileName = "Select File" - Makes the file name in the OpenFileDialog say Select File
OpenFileDialog1.ShowDialog() - Shows the OpenFileDialog
pic = New Bitmap(OpenFileDialog1.FileName) - Adds the value of the selected image to pic
PictureBox1.BackgroundImage = pic - Changes the image in ImageBox1 to pic

End Sub

End Class

Step 6: Save and Test

Picture of Save and Test

Save your program (File->Save All) and click the green play button on the toolbar to debug your program. If all goes well then you should be able to use the program.

The final program is stored in 'My Documents/Visual Studio 2005/Projects/PROJECT NAME/PROJECT NAME/Bin/Debug/PROJECT NAME.exe'
(Where PROJECT NAME is the name of the project)

Step 7: Final Thoughts

Picture of Final Thoughts
That's it!
Congratulations on creating your very first program in visual basic. It wasn't so hard now - was it?

Now you can go onto making more complex programs, or you can edit this one. I made some modifications to my program:
  • I changed the background color
  • I added some copyright info
In fact - here is some homework for you: see if you can get the program to change size for the image
  • HINT: You do it using the properties window.

Good Luck!

Comments

waterlubber (author)2012-01-31

cool. first pic viewer that worked. unlike microsoft's example...:)PS had to double click button instead of adding code to avoid those pesky errors.
BTY Figured out had do to that auto size thing. set size to AutoSize. Easy. Same with copy right.
Use this:
MessageBox.Show("YOUR MESSAGE HERE")
That puts a pop-up!

megatjt (author)2010-10-24

here is virtual basic expres edition free

http://en.kioskea.net/download/download-104-visual-basic-express-edition

Fox-san48 (author)2010-05-14

very nice, i just made the hello world thing in vb express 2008 that, i think is the best place to start, i'll try yours now though, thanks :D

Colonel88 (author)2009-07-03

It doesnt work because if you play it there is a ton of errors. Bad code.

Colonel88 (author)Colonel882009-07-03

"pic" is not declared. How to make it declared???

munchman (author)Colonel882009-07-03
Pic is declared by the line
Private pic As bitmap
Rockerx (author)2008-07-23

I just thought It would be funny to make a program that had a list of buttons and when you press one it made the sound that it had labeled on it. I found a cd of about 200 different sounds when this came to mind. I was about to do it but got stuck. How do you make a button make sound when you hit it and not do anything else?

munchman (author)Rockerx2008-07-24

hmm... I know you can embed a windows media control into a form... I'll tell you what... I'll look around for you and PM you with the answer. Thanks, Munchman

Rockerx (author)munchman2008-07-24

alright thanks!

awang8 (author)Rockerx2009-01-23
It is possible to do this. First download a free converter and convert all your files to .wav (I'm not responsible for any viruses). In a new form add all the buttons you want and name them. Then double-click of their button. Some code should appear. Inbetween the two lines add:
My.Computer.Audio.Play(My.Resources.filenamehere, AudioPlayMode.Background)
replacing filenamehere with the name of your audio file (don't write .wav). Repeat for each button. You should now have about a hundred errors. Now, to fix those errors go to Project - Properties and click on the resources tab. Click add resource and select your file. Repeat for every song. All errors should disappear. Now debug your program and check to see if it works.

If it does: Hooray! You're finished. Now publish it and install.

If it doesn't: Make sure you done everything correctly. If it still doesn't work, get a hammer and smash your "I won't work for you" computer.
animan1 (author)awang82009-06-23

you dont need a converter!!! Goto www.mediaconvert.com

munchman (author)animan12009-06-24

PSST: That's a converter, just an online one, that would chew into your usage limit if you use it a lot

animan1 (author)munchman2009-06-30

no its unlimited free and online i use it all the times

munchman (author)animan12009-06-30

But still a converter. And because it's online if you use it enough your internet usage will be eaten up (excess usage fees or even worse... shaping! )

munchman (author)awang82009-01-25

That's a good idea. And, you just taught me resources. Thanks!

awang8 (author)munchman2009-01-25

Happy to help.

ReCreate (author)2009-05-13

How Do i register Visual Basic? I need a key,Since this is free

munchman (author)ReCreate2009-05-14

You shouldn't need a key. Make sure you get the express edition, not Visual Studio, you have to pay for the latter.

ReCreate (author)munchman2009-05-14

When i click on Help > Register product On the top it says Visual Studio I downloaded exactly what you have here,Whats going on?

munchman (author)ReCreate2009-05-14

Okay, I understand what is happening. Microsoft makes a product known as Visual Studio, it is a collection of VB.NET, Visual C# and Visual C++ in the one product. It costs around about $299 US for the Professional Edition. Visual Studio Express Editions (here is where my knowledge ended until I researched this) is these languages broken into separate products (ie VB.NET Express Edition) I didn't realise that they still came under the Visual Studio banner. Bottom line is, it is free, and sorry for the confusion.

ReCreate (author)munchman2009-05-14

Well,Why is it telling me i have to register in 28 Days now?

munchman (author)ReCreate2009-05-14

Hmm, that has never happened to me... Maybe Microsoft wants people to upgrade... Try downloading the latest version link

ReCreate (author)munchman2009-05-15

Argh...

munchman (author)ReCreate2009-05-15

Oh, your screenshot just made it click! You don't need to register, It just allows you to access things like program updates and whatnot. You don't need to do it, and if you don't the program isn't limited in any way. Just ignore it, the program will be fine. Once again, sorry for the confusion :)

munchman (author)munchman2009-05-15

Sorry, not program updates, you get things such as added extra tools etc

ReCreate (author)munchman2009-05-15

Ok,Good.

ReCreate (author)munchman2009-05-15

Can You Rephrase that first sentence? And your Saying that registration is for something else? Whew,Good.

ReCreate (author)munchman2009-05-15

Hmm.Ill try

ReCreate (author)munchman2009-05-14

Yes,I have Visual Basic 2005 Express edition,Is it completely free?

ReCreate (author)2009-05-12

Nice,Downloaded,Installed it,Now i will have to wait till tomorrow,Its one o'clock AM

ReCreate (author)ReCreate2009-05-13

Ah,Im back,Now i checking out the tut ;)

emptyport (author)2009-02-22

Great Instructable. I am a complete noob at this stuff but I had no problem creating this program.

jeffconnelly (author)2008-12-11

You got the picture on step 6 from www.imagechef.com I was just on there.

munchman (author)jeffconnelly2008-12-11

No, it is my avatar. I got it from the MSN avatar website.

munchman (author)munchman2008-12-11

Although, looking at that site, I can stop using my low quality screenshot from msn (don't tell them I stole it.)

lolcat360 (author)2008-08-20

does the free version have any viruses?

MasterMediaXD (author)lolcat3602008-10-17

No

Th3Snip3r (author)lolcat3602008-08-30

Its from Microsoft, No, it doesn't.

nattyee (author)2008-07-25

that's awesome munchman

twenglish1 (author)2008-05-13

cool instructable i made one on how to make a chat program in visual basic check it out

MODX (author)2008-04-01

very nice! Great instructable!

munchman (author)MODX2008-04-03

Thanks!

flames10391 (author)2007-12-19

don't you have to "dim" private pic as bitmap? i'm in computer programming at my school and we use vb2005 and to use a variable we use "dim x as integer" FOR EXAMPLE

Noob (author)flames103912008-03-05

"Private" is also used for declaring a variable

GorillazMiko (author)2007-12-19

ill make a virus! just kidding haha cool instructable

munchman (author)GorillazMiko2007-12-19

It's good for making somthink that looks like a virus.

ac1D (author)2007-12-19

Nice instructable, but you should explain the code, if you want ppl to learn!

munchman (author)ac1D2007-12-19

Thanks, I will add it to the instructable when I get a chance.

munchman (author)munchman2007-12-19

Added. Tell me if you think I should break it into a new step...

ac1D (author)munchman2007-12-19

Thanks, it look nice and it's very instructible :)

About This Instructable

88,837views

29favorites

License:

Bio: I like doing things. Whether it be blowing stuff up or creating a webpage.
More by munchman:A new show, and how to jump out of an exploding building without killing yourself.How to make a tropical smoothieElectronic Drum Kit
Add instructable to: