You are on page 1of 10

c 


    
Jul 30, 2009 Dan Killam @ 3:57 am GMT

JY à T

0diggsdigg

This tutorial is geared to teach individuals who are brand new to àisual asic 2008 and want to
create their first program in the language The tutorial will cover proper naming styles,
transferring information to a label, creating the code to execute the program on a button click,
creating shortcuts, commenting, managing the tab order, and exiting an application
toc_collapse=0;
Contents [hide]

JY Creating the Form


JY Creating Shortcuts
JY Conventional aming
JY xiting the Application
JY ecording and Displaying the Data
JY Clear the text boxes
JY Tab Order

àisual asic (à  is a very powerful language, that makes creating programs quick and simple
à has a GUI interface that allows the user to drag and drop tools that they want to use, such as
a textbox, or label A major advantage is that this saves a lot of time, in C++ or other languages
you would need to hard code a date-time picker, yet à provides that for you with customizable
options and presents it in a pleasant way Another great feature is that à recognizes where you
have placed other items and can automatically align your next item with previous ones

à isn't just drag and drop, to really make a great program though you're going to have to write
your own code, and à seamlessly switches between the two views

Let's get started!

£  

Go to File > ew Project and select Windows Form Application


D  
    
ame your program, select a location to save it and click OK
]ou should now have "Form1" and a à layout similar to mine

I am going to make a program that will record prices, dates, and descriptions of items I buy at a
garage sale and sell on e ay
      

    
  
 
        
       

The fields of information I wish to record are: the date I bought the item, how much I paid for it,
a description of the item, the date I sold it, how much I sold it for, my shipping cost, the cost I
charged to ship it, and an ebay listing fee

ow that I know what I want in my form, I can start working on the layout Start your layout
now

Here is how mine looks, take a look at it, and then I will explain why I chose to lay it out the way
I did
Starting at the top of the image, you'll notice that I have changed the name of my form which is a
property available in the Properties menu (off to the right The next arrow points to a textbox,
which is available in the Toolbox (to the left Also available to us via the Toolbox are the date
time pickers, labels, and buttons - if you're having trouble locating these items in the Toolbox,
they are sorted in alphabetical order and are located under Common Controls To change the
label's text from Label1, change the Text property under appearance to a descriptive label - the
same applies for the buttons The last arrow points to a shortcut that I have included to allow me
to jump to the e ay Listing Fee textbox

£   

To create a shortcut, select your label, and in the label name place and ampersand (& before the
letter you want the shortcut to be assigned to For example, the " " in e ay Listing Fee is the
shortcut so I have e& ay Listing Fee as my text property To quickly jump to the textbox once
my program is running all I need to do is hold Alt + - this is another useful feature à makes
simple to implement

         
 
   D  
    
      
      
  

While viewing my current layout, you may notice that it is evenly spaced, and aligned à
allows me to change the location (as well as size of each item on the form so I spaced each item
equally

£ 
ow, we aren't done quite yet with the layout, but we are close and will begin coding the back-
end shortly What we need to do now is create labels for the entered information to be stored in,
and give proper design names to the labels and textboxes

The à naming conventions can be viewed here on Microsoft's website The ones we will be
using are dtp for DateTime Picker, lbl for Label, txt for Textbox, and btn for utton I have a text
box for the item description, so the name for this would be: txtItemDescription - there are no
spaces allowed, and we need to start the name with a letter Likewise, the label for item
description would be lblItemDescription

I have given all my labels and textboxes their proper names To hide the labels I will be storing
the entered data in, I have cleared the text fields and they will not be visible to the user until a
value has been entered in them When selecting all of my program (Ctrl + A I see this:

ÿ  
 
       
! 
"         

¦  

ow for the coding! The first part I am going to code, is the xit button because it requires the
least amount of work Double click the xit button and à will open the code view for you, with
some code already in there

o 


o     
  !"#

 

In the Private sub, type Applicationxit(
]ou may have noticed à 's auto-complete feature which can be quite useful when you aren't
100% sure which features are available to an item or function In this case, xit( is a function
already written for us by the guys at Microsoft and we just put that code in the button's sub If
you run your program (F5 and hit the exit button (or Alt + x it will close the application

This is what you should have:

o 


o     
  !"#
$$
"
 


While this does what we want, it is not complete because there is no commenting to explain what
this Sub does Applicationxit( is self-explanatory, but you should get into the habit of
commenting your Sub's To explain what a sub does, I like to do block commenting which you
can view in the next code section In à you may comment using an apostrophe (' and writing
your comment after If you want to comment out many lines you can select the text, and click the
comment button

"       #        
  

     

Alright, now we're going to code the ecord button, so go back to design mode (through the tab
at the top and double click the ecord button It will then place the code for that button
underneath the xit button code

o 


o     
  !"#
%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&
%&'
$
!  %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&
$$
"
 

o  (
   
  !"#(


 


This upcoming part will be the meat of the program, but don't worry it's simple!

We want to send the data entered into the text boxes to the appropriate labels With the labels
and text boxes properly named, this step will be quick to complete thanks to the auto-complete
feature ]our text boxes and labels should be similar in names To send the data from the text
box to the label we will set the label equal to the text box -If we instead set the text box equal to
the label we would lose the data entered into the text box

o  (


   
  !"#(

%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&
%&(


 ' '

%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&
)*  +,$)*  +
o o+,o o+
)
+,$)
+
- ) $
+,- ) $
+

!o +,!o +
'$$!o +,'$$!o +
'$$!
+,'$$!
+
 +, +
 

£    

If we wanted to enter multiple items, we would not want to have to clear the data fields each
time, so we can set the program to do it for us by setting the values equal to nothing nter this
code below "lble ayFeeText = txte ayFeeText", but above "nd Sub"

%& 

o o+,..
- ) $
+,..
!o +,..
'$$!o +,..
'$$!
+,..
 +,..

ÿ          $ 

     
 
 

  
       

r 

Tab order is very simple to manage While in Design àiew (viewing the layout, not the code go
to àiew > Tab Order Here is mine:

Create a logical order for the program to tab through by clicking in the order you want the user to
tab through The only boxes that matter while doing this, are the ones that the user puts
information in, the labels containing text do not need to be touched Press sc when you're done
editing the tab order
Congratulations on finishing the tutorial Here is an image of the programming running after
entering an item, as well as my final code

o 


o     
  !"#
%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&
%&'
$
!  %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&
$$
"
 

o  (
   
  !"#(

%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&
%&(


 ' '

%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&
)*  +,$)*  +
o o+,o o+
)
+,$)
+
- ) $
+,- ) $
+

!o +,!o +
'$$!o +,'$$!o +
'$$!
+,'$$!
+
 +, +

%& 

o o+,..
- ) $
+,..
!o +,..
'$$!o +,..
'$$!
+,..
 +,..
 

Y

You might also like