39
Visual Basic: Introduc1on Object Oriented Programming in VB.NET

Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Embed Size (px)

Citation preview

Page 1: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Visual  Basic:    Introduc1on  

Object  Oriented  Programming  in  VB.NET  

Page 2: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Visual  Studio.NET  

¡ A  pla@orm  that  allows  the  development  and  deployment  of  desktop  and  web  applica1ons    

¡ Allows  user  choice  of  many  .NET  languages  § May  program  in  One  of  them  § May  create  different  parts  of  applica1on  in  different  languages  ▪  Visual  Basic  ▪  C#  (C  Sharp)  ▪  C++  ▪  J++  ▪  Etc.  

Page 3: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Visual  Studio.NET  

¡  Integrated  Development  Environment  –  allows  the  automa1on  of  many  of  the  common  programming  tasks  in  one  environment  § Wri1ng  the  code  §  Checking  for  Syntax  (Language)  errors  §  Compiling    and  Interpre1ng(Transferring  to  computer  language)  

§  Debugging  (Fixing  Run-­‐1me  or  Logic  Errors)  §  Running  the  Applica1on  

Page 4: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

What  is  Visual  Basic.Net  

¡ 4th  Genera1on  Programming  Environment  /  Development  Language  

¡ Based  on  BASIC  language  §  Beginners  All-­‐Purpose  Symbolic  Instruc1onal  Code  

¡ Most  widely  used  tool  for  developing  Windows  Applica1ons  § Graphical  User  Interface  (GUI)  § Menus,  Bu\ons,  Icons  to  help  the  user  

¡ Full  Object-­‐Oriented  Programming  Language  

Page 5: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

How  a  VB  Applica1on  is  Compiled  and  Run  

Solution

.NET FrameworkVisual Studio .NET

Project

CommonLanguageRuntime

IntegratedDevelopmentEnvironment

Source files

Visual Basiccompiler

1 2 3

Assembly

Intermediate Language (IL)

Class references

Page 6: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Project  and  Solu1on  Concepts  

•  User  creates  a  new  project  in  Visual  Studio  –  A  solu1on  and  a  folder  are  created  at  the  same  1me  with  the  same  name  as  

the  project  –  The  project  belongs  to  the  solu1on  –  Mul1ple  projects  can  be  included  in  a  solu1on  

•  Solu1on  –  Contains  several  folders  that  define  an  applica1on’s  structure  –  Solu1on  files  have  a  file  suffix  of  .sln  

•  Project:  contains  files  for  a  part  of  the  solu1on  –  Project  file  is  used  to  create  an  executable  applica1on  –  A  project  file  has  a  suffix  of  .vbproj  –  Every  project  has  a  type  (Console,  Windows,  etc.)  –  Every  project  has  an  entry  point:    A  Sub  procedure  named  Main  or  a  Form  

Page 7: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Project  and  Solu1on  Folders/Files  •  Solu1on  folder  

– Solu1on  file  (.sln)  – Project  folder  

•  Project  file  (.vbproj)  •  Visual  Basic  source  files  (.vb)  •  My  Project  folder:  contains  configura1on  informa1on  common  to  all  projects  

–  The  file  AssemblyInfo.vb  contains  assembly  metadata  –  The  References  folder  contains  references  to  other  assemblies  

•  The  bin  folder  contains  the  executable  file  produced  as  a  result  of  compiling  the  applica1on  

Page 8: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Using  Visual  Studio.NET  

Page 9: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Crea1ng  an  Applica1on  •  Select  the  “Create  Project”  op1on    from  the  “Recent  

Projects”  box  on  the  Start  Page  

Page 10: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Default  Se`ngs  

Page 11: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Visual  Basic  Forms  

•  This  is  a  Visual  Basic  GUI  object  called  a  form  

•  Forms  are  the  windows  and  dialog  boxes  that  display  when  a  program  runs.  

•  A  form  is  an  object  that  contains  other  objects  such  as  bu\ons,  text  boxes,  and  labels  

Page 12: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Visual  Basic  Controls  

•  Form  elements  are    objects  called  controls  

•  This  form  has:  – Two  TextBox  controls  – Four  Label  controls  – Two  Bu1on  controls  

•  The  value  displayed  by    a  control  is  held  in  the  text  property  of  the  control  

•  Lea  bu\on  text  property  is  Calculate  Gross  Pay  •  Bu\ons  have  methods  a\ached  to  events  

Page 13: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Design  Window  T

oolbox  

Solu1on  Explorer  

Proper1es  Window  

Page 14: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Crea1ng  the  Applica1on  

•  Step  1:    Add  a  Control  to  the  Form  –  Bu\on  – Look  in  the  Toolbox  for  the  Bu\on  Control  – Select  the  Bu\on  with  the  Mouse  – Draw  a  Rectangle  Region  in  the  Design  Window  by  holding  the  mouse  bu\on  down  

– Release  the  mouse  bu\on  to  see  your  bu\on  –  (Can  also  be  added  by  double  clicking  on  the  bu\on  in  the  Toolbox)  

Page 15: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related
Page 16: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Crea1ng  the  Applica1on  

•  Add  a  Second  Bu\on  to  the  Form  •  Put  it  in  the  lower  right  corner  

•  The  project  now  contains  •         a  form  with  2  bu\on    •         controls  

Page 17: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Control  Proper1es  

•  Proper1es  – All  controls  have  proper1es  – Each  property  has  a  value  (or  values)  – Determine  the  Look  and  Feel  (and  some1mes  behavior)  of  a  Control  

– Set  ini1ally  through  the  Proper1es  Window  

•  Proper1es  Set  for  this  Applica1on  – Name  – Text  

Page 18: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Name  Property  

•  The  name  property  establishes  a  means  for  the  program  to  refer  to  that  control  

•  Controls  are  assigned  rela1vely  meaningless  names  when  created  

•  Change  these  names  to  something  more  meaningful  

•  Control  names  must  start  with  a  le\er  •  Remaining  characters  may  be  le\ers,  digits,  or  underscore  

Page 19: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Examples  of  Names  

btnCalcGrossPay btnClose

txtHoursWorked

txtPayRate

lblGrossPay

Label1

Label2

Label3

§  The label controls use the default names (Label1, etc.)

§  Text boxes, buttons, and the Gross Pay label play an active role in the program and have been changed

Page 20: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Control  Naming  Conven1ons  

•  Should  be  meaningful  •  1st  3  lowercase  le\ers  indicate  the  type  of  control  

–  txt…  for  Text  Boxes  –  lbl…  for  Labels  – btn…  for  Bu\ons  

•  Aaer  that,  capitalize  the  first  le\er  of  each  word  •  txtHoursWorked  is  clearer  than  txthoursworked  

•  Change  the  name  property  – Set  the  name  of  bu\on1  to  btnWelcome  – Set  the  name  of  bu\on2  to  btnExit  

Page 21: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Se`ng  Control  Proper1es  

•  Click  on  the  Control  in  the  Design  Window  •  Select  the  appropriate  property  in  the  Proper1es  Window  

Page 22: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Text  Property  

•  Determines  the  visible  text  on  the  control  •  Change  the  text  property  

– bntWelcome  à  set  to  “Say  Welcome”  – btnExit  à  set  to  “Exit”  

– Do  not  need  to  include  the  “ “  in  your  text  field  – No1ce  how  the  bu\ons  now  display  the  new  text  

Page 23: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Event  Driven  Programming  

•  The  GUI  environment  is  event-­‐driven  •  An  event  is  an  ac1on  that  takes  place  within  a  program  – Clicking  a  bu\on  (a  Click  event)  – Keying  in  a  TextBox  (a  TextChanged  event)  

•  Visual  Basic  controls  are  capable  of  detec1ng  many,  many  events  

•  A  program  can  respond  to  an  event  if  the  programmer  writes  an  event  procedure  

Page 24: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Event  Procedures  •  An  Event  Procedure  is  a  block  of  code  that  executes  only  when  par1cular  event  occurs  

•  Wri1ng  an  Event  Procedure  – Create  the  event  procedure  stub  

•  Double  click  on  control  from  Design  Window  –  for  default  event  for  that  control  

OR  •  Open  the  Code  Editor  (F7  or  View  Menu/Code  op1on)  •  Select  Control  &  Select  Event  from  drop  down  windows  in  Code  Editor  

– Add  the  event  code  to  the  event  procedure  stub      

Page 25: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Open  the  Code  Editor  

Page 26: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related
Page 27: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Select  the  Control  for  the  Event  Procedure  

•  Select  the  btnWelcome  control  from  the  Form  Controls  List  Box  

Page 28: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Select  the  Event  for  the  Event  Procedure  

•  Select  the  Click  event  from  the  list  of  many  available  events  

•  Bu\ons  have  57  possible  events  they  can  respond  to  

Page 29: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Event  Procedure  Stub  •  Beginning  of  Procedure  is  created  for  you  

–  If  you  create  stub  by  double  clicking  on  control  it  will  create  a  stub  for  the  most  commonly  used  event  for  that  control  

Page 30: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Add  the  Event  Code  

•  Write  the  code  that  you  want  executed  when  the  user  clicks  on  the  btnWelcome  bu\on  – Type:    MsgBox  (“Welcome  to  Visual  Basic”)  

– Must  be  contained  within  the  Event  Procedure  Stub  

Page 31: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Wri1ng  Visual  Basic  Code  •  Not  Case  Sensi1ve  

– Visual  Basic  will  “correct”  case  issues  for  you  •  Keywords  are  in  Blue  

– Special  reserved  words  •  Comments  in  Green  •  Problems  with  Syntax  (Language)  will  be  underlined  in  blue  

Page 32: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Coding  Conven1ons  

•  Rules  – Use  spaces  to  separate  the  words  and  operators  –  Indenta1on  and  capitaliza1on  have  no  effect  

•  Recommenda1ons  – Use  indenta1on  and  extra  spaces  for  alignment  – Use  blank  lines  before  and  aaer  groups  of  related  statements  

– Code  all  variable  declara1ons  at  the  start  of  the  procedure  

– Group  related  declara1ons  

Page 33: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Comments  •  Usage  

– Type  an  apostrophe  (  '  )  followed  by  the  comment  – The  compiler  ignores  everything  on  the  line  aaer  ‘  – Used  for  documenta1on/readability  and  to  disable  chosen  statements  during  tes1ng  

•  Recommenda1ons  – Follow  apostrophe  with  a  star  for  readability  (  ‘*  )  – Use  at  beginning  of  program  to  indicate  author,  purpose,  date,  etc.  

– Use  for  groups  of  related  statements  and  por1ons  of  code  that  are  difficult  to  understand  

Page 34: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Code  that  follows  recomenda1ons  '*  ======================================  '*  Class:            CIS  115-­‐101  '*  Author:      Paul  Overstreet  '*  Purpose:  Homework  1  –  VB  ApplicaDon  '*  Date:              11/30/01  '*  ======================================  

Public  Class  Form1    

               Private  Sub  btnCalculate_Click(ByVal  sender  As  System.Object,  ByVal  e  As  System.EventAr…      

                               '*Variable  declaraDons                                  Dim  dOrderTotal  As  Decimal                                  Dim  dDiscountAmount  As  Decimal    

                               '*Get  total  from  textbox                                      dOrderTotal  =  txtOrderTotal.Text    

                               '*Calculate  the  proper  discount                                  dDiscountAmount  =  dOrderTotal  *  0.25  '                              dDiscountAmount  =  dOrderTotal  *  0.25    

               End  Sub                  

               Private  Sub  BuYon1_Click(ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)…      

                                 ‘*Code  goes  here  

               End  Sub    

End  Class  

Page 35: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Create  Event  Procedure  for  Exit  Bu\on  

•  Create  an  Event  Procedure  for  when  the  btnExit  bu\on  is  clicked  

•  Have  it  display  “Goodbye”  in  a  MsgBox  •  Then  “End”  –  this  will  terminate  the  program  

Page 36: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Switching  to  Design  Window  

•  You  can  switch  between  the  Design  Window  and  the  Code  Window  (once  opened)  by  clicking  on  the  tabs  at  the  top  of  the    

•         Design  and  Code  Windows  – Form1.vb(Design)  is  the    –         design  window  – Form1.vb  is  the  Code  Window  

Page 37: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Running  the  Applica1on  

¡ Click  the  Run  Icon  on  the    Standard  Toolbar    

¡ Or  Press  F5  

¡ This  will  begin  the  program  ¡ Display  the  Form/Window  ¡ Nothing  will  happen  § Wai1ng  on  an  Event  

Page 38: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Test  the  Events  

•  Click  on  the  “Say  Welcome”  bu\on  – The  message  box  should  display  

•  Click  on  the  “Exit”  bu\on  – The  message  box  should  display  – The  applica1on  should  terminate  

Page 39: Visual’Basic:’’Introduc1on’ · Label3 ! The label controls use the default names (Label1, etc.) ! ... – Use’blank’lines’before’and’aer’groups’of’related

Save  the  Project  

•  Make  sure  to  save  your  work  – SAVE  ALL  (not  Save  Form)  – Visual  Basic  applica1ons  are    –   made  of  several  files  -­‐    –         Oaen  even  several  forms