Docstoc

SCROLL BAR INPUT & CALCULATION – V. Basic 2005 Tutorial Exit

Document Sample
SCROLL BAR INPUT & CALCULATION – V. Basic 2005 Tutorial Exit Powered By Docstoc
					        SCROLL BAR INPUT & CALCULATION – V. Basic 2005 Tutorial
PROBLEM:
How might an electronic screen accept a user’s input of a number of degrees of air temperature,
Celsius or Fahrenheit, and show the converted value in the alternate temperature scale?

SOLUTION STORYBOARD:
                                                             Form – User interface with
             Temperature Converter                                  envelope icon

                                             Exit             Button – Click event to exit form

      Drag scroll bar button to change                        Label – Displays instruction

                 Celsius           Fahrenheit              Labels – Display temperature scales

                    0                  32                  Labels – Display temperature values

                                                             Horizontal scroll bar –
                                                             Accept user temperature input

VISUAL BASIC INTERFACE:

#1 Launch Microsoft Visual Basic 2005.
#2 Click <File>, then <New Project…>, then choose <Windows Application>.
      Change the <Name:> from Windows Application1 to VB_Tut2Temperature.
      Click <OK> to create the new project. (It is not yet saved.)
#3 Click <File>, then <Save All>.
      Ensure <Location:> shows the path to the file’s <Name:>, VB_Tut2Temperature,
      in the Home directory, then click <Save>.
#4 Click the <View> menu, then, if it is not already open, click <Solution Explorer> and
   drag it to the right-of-screen.
   Click the <View> menu, then, if it is not already open, click <Properties Window> and
   drag it to the right-of-screen beneath the Solution Explorer.
   Click the <View> menu, then, if it is not open, click <Toolbox> and drag it to the right.




#5 Click <File>, then <Save Form1.vb As…> in the VB_Tut2Temperature folder in the
      Home directory as FormTemperature.vb. Click <Save>.
      ( <Save> <Save All> may be used to update any changes made within the project.)


VB_Tut2Temperature.doc                                                           M. J. Onley
SOLUTION INTERFACE:

#6 Click in the form – to make it active – then use the Properties Window to set the following
       values to properties:
               (Name)          frmTemperature                 BackColor         <Web> LightBlue
               Size        660, 460                           Text       Temperature Converter
               Icon        <Local Disk(C:) \ Program Files \ Microsoft Office \ OFFICE11 \
                                FORMS \ 1033> SIGNL.ICO
#7 Scroll down the Toolbox to find the Button control, click it, then click anywhere inside the
       form to place a button on the form.
       (The Properties Window shows that the new button, Button1 is active.)
       Use the Properties Window to set the following values to properties:
               (Name)          btnExit                        Text       Exit
               Font        Arial, 14Pt, Bold                  BackColor         <Web> Gainsboro
               Location          520, 32                      Size       120, 50
#8 Scroll down the Toolbox to find the Label control, click it, then click anywhere inside the
       form to place a label on the form.
       Use the Properties Window to set the following values to properties:
               (Name)          lblInstruction                 Location        8, 104
               Font        Arial, 20Pt, Bold                  BackColor         <Web> LightCyan
               Text        Drag scroll bar button to change temperatures.
       (A Label is a better choice than a Textbox where it is undesirable that the user be able
       to type into the control at run-time.)
#9 Place another label on the form, then set the following values to properties:
               (Name)          lblCelsiusTitle                Location        104, 184
               Font        Arial, 20Pt, Bold                  BackColor         <Web> LightCyan
               Text        Celsius
#10 Place another label on the form, then set the following values to properties:
               (Name)          lblCelsiusValue         Location       104, 240
               Font        Arial, 28Pt, Bold                  BackColor         <Web> LightCyan
               TextAlign          MiddleCenter                BorderStyle         Fixed3D
               Text        0
#11 Place another label on the form, then set the following values to properties:
               (Name)          lblFahrenheitTitle             Location        408, 184
               Font        Arial, 20Pt, Bold                  BackColor         <Web> LightCyan
               Text        Fahrenheit
#12 Place another label on the form, then set the following values to properties:
               (Name)          lblFahrenheitValue             Location        408, 240
               Font        Arial, 28Pt, Bold                  BackColor         <Web> LightCyan
               TextAlign          MiddleCenter                BorderStyle         Fixed3D
               Text        32
#13 Scroll down the Toolbox to find the HScrollBar control, click it, then click anywhere inside
       the form to place a horizontal scroll bar on the form.
       Use the Properties Window to set the following values to properties:
               (Name)          hsbTemperature                 Location        104, 328
               Size        464, 48                            Value        0
               Maximum            50                          Minimum          -10
       (The advantage of using a scroll bar control to receive input data from the user is that the
       user is restricted to a particular data type – numeric – and to a specified data range –
       -10 to 50 – in this case.                The user can only input data that is valid.)
       Click <File>, then <Save All> to save the updated application.

VB_Tut2Temperature.doc                                                            M. J. Onley
SOLUTION CODE:

#14 Double-click the Exit button, btnExit to open the Code Window.
      (Visual Basic shows the form, frmTemperature, as a class and shows an incomplete sub-
      procedure as the event handler for the Click event for btnExit.)
      Between the lines of code – Private Sub … – and – End Sub – type the lines:
              ‘Close the window and exit the application.
              Me.Close()

         (The first line is a programmer’s comment, ignored by Visual Basic.)
         (Me refers to the form itself. Close() is a method which may be called to close a form.
         With no form window open, the application ends.)



                                                                               Click the tabs to
                                                                               switch between
                                                                               the Design and
                                                                               Code windows.


#15 Switch back to the Design Window, then double-click the hsbTemperature scroll bar.
      (The Code Window re-opens to show an incomplete sub-procedure for
      hsbTemperature_Scroll.)
      Between the lines of code – Private Sub … – and – End Sub – type the lines:
             ‘Display the Value property of hsbTemperature in the label, lblCelsiusValue.
             lblCelsiusValue.Text = Format(hsbTemperature.Value, “0”)
             ‘Convert the Value property of hsbTemperature from degrees Celsius to
             ‘degrees Fahrenheit and display this value in the label, lblFahrenheitValue.
             lblFahrenheitValue.Text = Format(9 / 5 * hsbTemperature.Value + 32, “0”)

         (Notice the syntax of the assignment statement – object2.property = object1.property )
         (Notice that / is used for division and * is used for multiplication.)
         (The Format function is used here to display the scroll bar value as an integer only.
         “0.00” would display 2 decimal places,
         “0.##” would display 2 decimal places optionally.)
         (The “Private” in “Private Sub” means that the sub-procedure may be accessed only
         Within this form if the project were to comprise several forms.)

SOLUTION TESTING:

#16 Switch back to the Design Window.
       Click <Debug>, then <Start Debugging> to start the project running and to test for any
       run-time coding errors or logical errors or display problems.
       Notice that      a) the maximum value of the scroll bar displays as 41, even though
                            it was set as 50 in the control’s properties window,
                        b) the form’s window does not hide the design environment behind it.
       Click the Exit button to close the form and end debugging.
#17 Click hsbTemperature, then change the value of its Maximum property from 50 to 59.
       (It is a peculiarity of both HScrollBar and VScrollBar controls that the maximum value
       property needs to be set to be 9 more than the highest value to be captured.)


VB_Tut2Temperature.doc                                                          M. J. Onley
#18 Click the form, then change the value of its WindowState property from Normal
       to Maximised.
#19 Use the Toolbox window to place a label on the form.
       Use the Properties Window to set the following values to properties:
               (Name)        lblAuthor                        Text      (your) Firstname Surname
               Font       Arial, 10Pt, Bold                   BackColor       White
               Location       (Drag into the bottom right corner of the window.)
#20 Click <Debug>, then <Start Debugging> to test the updated application.
       Drag the scroll bar button to note that the range of temperatures now permissible is from
       -10 to 50 degrees Celsius.
       Click the Exit button to close the form and end debugging.
       Click <File>, then <Save All> to save the updated application.
       Click <File>, then <Close Project> to close the updated application.
       Click <File>, then <Exit> to close Visual Basic.
       (To re-open the application in Visual Basic click <File>, then <Open Project…>,
       double-click the folder VB_Tut2Temperature in the Home directory, then double-click the
       solution file VB_Tut2Temperature.sln.)




PRACTICE PROBLEM:

How might an electronic screen be set up to employ a pair of scroll bars to accept validated input
(from a user) of the radius and the height of a cylindrical rainwater tank, and to display those
values, as well as the calculated value of the volume of the tank?
The user would need advice on how to use the application.
The matter of the use of consistent units would also have to be considered.




VB_Tut2Temperature.doc                                                            M. J. Onley

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:13
posted:4/4/2010
language:English
pages:4
Description: SCROLL BAR INPUT & CALCULATION – V. Basic 2005 Tutorial Exit ...