36
27/06/22 27/06/22 1 3.3 Working with 3.3 Working with Data Data

06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

Embed Size (px)

Citation preview

Page 1: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

19/04/2319/04/23 11

3.3 Working with Data3.3 Working with Data

Page 2: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

2219/04/2319/04/23

Learning ObjectivesLearning Objectives

Explain the circumstances when the Explain the circumstances when the following might be useful:following might be useful: Disabling buttons and making text boxes & Disabling buttons and making text boxes &

labels invisible at design time;labels invisible at design time; Enabling buttons and making text boxes & Enabling buttons and making text boxes &

labels visible when particular events happen labels visible when particular events happen during run time. during run time.

Page 3: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

3319/04/2319/04/23

Learning ObjectivesLearning Objectives

Explain the TextChanged event.Explain the TextChanged event.

Explain what the Round and Random Explain what the Round and Random functions are for and how they are used.functions are for and how they are used.

State the general form of the code which State the general form of the code which will clear the text property of a control.will clear the text property of a control.

Page 4: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

4419/04/2319/04/23

ConstantsConstantsUsed when:Used when: The user does not provide the value.The user does not provide the value. You don’t want the value to change during the program.You don’t want the value to change during the program.

e.g. Const TaxRate = 0.25e.g. Const TaxRate = 0.25

If you try to assign another value to a constant later in the program an error will occur.If you try to assign another value to a constant later in the program an error will occur.

Page 5: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

5519/04/2319/04/23

ConstantsConstantsUsing a constant rather than a number presents two advantages:Using a constant rather than a number presents two advantages: It allows the value to be changed by the programmer easily i.e. otherwise the It allows the value to be changed by the programmer easily i.e. otherwise the

programmer would have to go through and change the value every time it programmer would have to go through and change the value every time it occurs.occurs.

It makes the code more meaningful to anybody reading it.It makes the code more meaningful to anybody reading it.

Page 6: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

Arithmetic & Order of PrecedenceArithmetic & Order of PrecedenceOperatorOperator Visual Visual

BasicBasicOrder of Order of

precedenceprecedence

Exponentiation Exponentiation (e.g. 10(e.g. 102 2 = 10^2= 10^2 )) ^̂ 11

NegationNegation -- 22

MultiplicationMultiplication ** 33

DivisionDivision // 33

Integer Division Integer Division (produces a whole (produces a whole number by removing any decimal parts)number by removing any decimal parts)

\\ 44

Modulus Modulus (gives a remainder after (gives a remainder after division e.g.12 Mod 5 = 2)division e.g.12 Mod 5 = 2)

ModMod 55

AdditionAddition ++ 66

SubtractionSubtraction -- 66

Page 7: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

7719/04/2319/04/23

Clearing the text property of a Clearing the text property of a controlcontrol

….Text = “”

Name of controlName of control

Page 8: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

8819/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

SpecificationSpecification:: Allow the user to enter as many exam Allow the user to enter as many exam

marks as they wish and display them in a marks as they wish and display them in a list box. list box.

The program finishes when the user The program finishes when the user clicks a button to show the average mark clicks a button to show the average mark rounded to 2 decimal places.rounded to 2 decimal places.

Allow the user to use automatically Allow the user to use automatically generated random marks.generated random marks.

Page 9: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

9919/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Create a new project named ‘Average’.Create a new project named ‘Average’.

Change the form’s Change the form’s TextText property to property to ‘Calculating the average exam mark’.‘Calculating the average exam mark’.

Page 10: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

101019/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Place 3 Place 3 LabelsLabels, 1 , 1 TextBoxTextBox, 4 , 4 ButtonsButtons and 1 and 1 ListBoxListBox on the form (and follow the instructions on the form (and follow the instructions on the next slide) as shown below.on the next slide) as shown below.

Page 11: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

1111

Properties to set:Properties to set:Label1:Label1:

Name: Name: lblMarklblMark TextText: Enter exam mark: Enter exam mark BoldBold: True: True SizeSize: 12: 12Label2: Label2:

Name: Name: lblMeanlblMean TextText: Mean exam mark: Mean exam mark BoldBold: True: True SizeSize: 12: 12 VisibleVisible: False: FalseLabel3:Label3:

NameName: lblMeanResult: lblMeanResult SizeSize: 12: 12 VisibleVisible: False: FalseTextBox1:TextBox1:

NameName: txtMark: txtMark SizeSize: 12: 12

Button1Button1:: NameName: butOK: butOK TextText: OK: OK SizeSize: 12: 12 BoldBold: True: True EnabledEnabled: False: False

Button2Button2:: NameName: butMean: butMean TextText: Show Mean: Show Mean SizeSize: 12: 12 BoldBold: True: True Enabled: FalseEnabled: False

Button3Button3:: NameName: butQuit: butQuit TextText: Quit: Quit SizeSize: 12: 12 BoldBold: True: True

Button4Button4:: NameName: butRandom: butRandom TextText: Random Mark: Random Mark SizeSize: 12: 12 BoldBold: True: True EnabledEnabled: False: False

ListBox1ListBox1 NameName: lstMarks: lstMarks SizeSize: 12: 12

Page 12: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

121219/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

The reason for disabling the butOK is that The reason for disabling the butOK is that the user should not be able to click it until the user should not be able to click it until he/she has entered a mark.he/she has entered a mark.

The reason for making lblMean & The reason for making lblMean & lblMeanResult invisible is that they are lblMeanResult invisible is that they are useless until he/she has entered a mark.useless until he/she has entered a mark.

Page 13: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

Program 3.3 AverageProgram 3.3 Average

Two variables ‘Two variables ‘TotalTotal’ & ‘’ & ‘NumberOfMarksNumberOfMarks’ will be used by the butOK & ’ will be used by the butOK & butMean procedures so declare them globally outside these butMean procedures so declare them globally outside these procedures procedures (e.g. Double click the OK button, move up out of its procedure and create (e.g. Double click the OK button, move up out of its procedure and create a blank line in the Form declarations area):a blank line in the Form declarations area):

DimDim Total Total AsAs Integer Integer ‘Declares a variable to store running total of marks. It ‘Declares a variable to store running total of marks. It needs to be declared globally, as more than one button needs (in this case needs to be declared globally, as more than one button needs (in this case two – butOK & butMean) so it needs to continue to exist while the program two – butOK & butMean) so it needs to continue to exist while the program is running and its value “remembered” between clicks of either button.is running and its value “remembered” between clicks of either button.

DimDim NumberOfMarks NumberOfMarks AsAs Integer Integer ‘Declares a variable to store the number of ‘Declares a variable to store the number of exam marks entered. It needs to be declared globally, as more than one exam marks entered. It needs to be declared globally, as more than one button needs (in this case two – butOK & butMean) so it needs to continue button needs (in this case two – butOK & butMean) so it needs to continue to exist while the program is running and its value “remembered” between to exist while the program is running and its value “remembered” between clicks of either button.clicks of either button.

See next slide for a screenshot.See next slide for a screenshot.

Page 14: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

141419/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Page 15: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

151519/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

When the OK button is clicked:When the OK button is clicked: The butMean button must be enabled The butMean button must be enabled

so the user can find the mean.so the user can find the mean. The exam mark entered by the user The exam mark entered by the user

must be removed and the cursor must be removed and the cursor positioned in it ready for another mark.positioned in it ready for another mark.

Page 16: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

161619/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Move back inside the butOK procedure and enter the Move back inside the butOK procedure and enter the following code:following code:

‘Declare local variable for a new mark. It can be declared locally as once the mark has been added on to the Total and the NumberOfMarks incremented by one, the mark itself is of no use so can be “forgotten”. Declaring it globally would make it difficult to find where the value was changed as more than 1 procedure could change the value, so difficult to re-use it in different procedures.

Dim NewMark As Integer

Page 17: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

171719/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Still inside the butOK procedure and enter the following Still inside the butOK procedure and enter the following code:code:

NewMark = txtMark.Text ‘Read exam mark. lstMarks.Items.Add (NewMark) ‘Copy it to the list box. Total = Total + NewMark ‘Add it to the running total of marks. NumberOfMarks = NumberOfMarks + 1 ‘Increase the number

of marks by 1. butMean.Enabled = True ‘Enable the Show Mean button. txtMark.Clear() ‘Clear out the old exam mark. txtMark.Focus ‘Place cursor ready for next mark.

Page 18: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

181819/04/2319/04/23

Program 3.3 AverageProgram 3.3 AverageGo back to design view and double click the Go back to design view and double click the ShowShow MeanMean

button and enter the following code in its button and enter the following code in its procedureprocedure template:template:

‘Declare a local variable for the mean. It can be declared locally as once the mean has been calculated and displayed it is no longer needed so it can be “forgotten”, at least as far as the program is concerned, as it is displayed on the form for the user to remember or see. Also only one button needs it, that is the “Show Mean” button. Declaring it globally would make it difficult to find where the value was changed as more than 1 procedure could change the value, so difficult to re-use it in different procedures.

Dim Mean As Decimal

Page 19: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

191919/04/2319/04/23

Program 3.3 AverageProgram 3.3 AverageStill in the Still in the ShowShow MeanMean button and enter the button and enter the following code in its following code in its procedureprocedure template:template:

Mean = Total / NumberOfMarks ‘Calculate Mean mark. lblMeanResult.Text = Mean ‘Display it. ‘Show label which displays mean mark. lblMeanResult.Visible = True lblMean.Visible = True ‘Show its label. ‘Disallows more marks to be entered. txtMark.Visible = False lblMark.Visible = False ‘Hides the label for txtMark. butOK.Enabled = False ‘Disable the OK button.

Page 20: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

202019/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Go back to design view and double click the Go back to design view and double click the QuitQuit button and enter the following code in its button and enter the following code in its procedureprocedure template:template:

Application.Exit()Application.Exit() ‘Exit the application.‘Exit the application.

Save and run the program. What is the Save and run the program. What is the problem?problem?

The OK button is disabled so the mark cannot be The OK button is disabled so the mark cannot be processed!processed!

We disabled it at design time so that the user cannot We disabled it at design time so that the user cannot click it before a mark is entered.click it before a mark is entered.

However, the program has not been told to enable it.However, the program has not been told to enable it.

Page 21: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

212119/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

We will use the We will use the TextChangedTextChanged event of event of txtMarktxtMark..

i.e. the OK button will be enabled when the contents i.e. the OK button will be enabled when the contents of txtMark changes of txtMark changes

Double click the Double click the txtMarktxtMark.. Notice VB has correctly chosen the Notice VB has correctly chosen the TextChangedTextChanged

event.event. Enter the following code in its Enter the following code in its procedureprocedure template:template:

butOK.Enabled = TruebutOK.Enabled = True

Double click the butOK.Double click the butOK.butMean.Enabled = TruebutMean.Enabled = True

Page 22: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

222219/04/2319/04/23

Round Round functionfunction

Used for rounding numbers and currency.Used for rounding numbers and currency.

Needs two arguments.Needs two arguments. Variable or constant to be formatted.Variable or constant to be formatted. The number of decimal places to be rounded to.The number of decimal places to be rounded to. Needs to be proceeded in VB by Math.Needs to be proceeded in VB by Math.

General Form:General Form:

Math.Round(Math.Round(VariableOrNumberToBeRoundedVariableOrNumberToBeRounded, , NumberOfDecimalPlacesToBeRoundedToNumberOfDecimalPlacesToBeRoundedTo))

Page 23: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

232319/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Double click the Double click the Random Random button and button and change the display line to.change the display line to. txtMark.Text = Int(Rnd() * 100 + 0) txtMark.Text = Int(Rnd() * 100 + 0) ' Generate a random ' Generate a random

value between 0 and 100. value between 0 and 100.

Page 24: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

242419/04/2319/04/23

Random Random functionfunction

Gives a random number between 0 and 1.Gives a random number between 0 and 1.

Written as Rnd() in VB.Written as Rnd() in VB.

To generate whole numbers between 2 specified To generate whole numbers between 2 specified values:values:

Int(Rnd() * Int(Rnd() * HigherValueHigherValue + + LowerValueLowerValue))e.g.e.g.

' To generate random value between 1 and 6. ' To generate random value between 1 and 6.

Int(Int((Rnd())*6 + 1))Int(Int((Rnd())*6 + 1))

Page 25: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

252519/04/2319/04/23

Program 3.3 AverageProgram 3.3 Average

Double click the Double click the Show MeanShow Mean button and button and change the display line to.change the display line to. lblMeanResult.Text = Math.Round(Mean, 2)lblMeanResult.Text = Math.Round(Mean, 2)

‘Display the variable Mean formatted to 2 ‘Display the variable Mean formatted to 2 decimal places.decimal places.

Page 26: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

Writing code which is easy to understand:Writing code which is easy to understand:

Sensible variable names.Sensible variable names.

Keywords in capitals.Keywords in capitals. So that the reader does not have to keep cross referencing with a table So that the reader does not have to keep cross referencing with a table

of variable names.of variable names.

Comments or Annotation.Comments or Annotation. To explain the logic of the code.To explain the logic of the code.

Indentation Indentation (this is done automatically by VB for you):(this is done automatically by VB for you):

To show the lines of the code that go together.To show the lines of the code that go together.For example:For example:

Private Sub butCalculateMean …Private Sub butCalculateMean …

Dim ….Dim ….

Mean = Total / NumberOfMarks Mean = Total / NumberOfMarks

lblMeanResult.Text = Mean.lblMeanResult.Text = Mean.

…….... End SubEnd Sub

2626

Page 27: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

272719/04/2319/04/23

Extension 1 Extension 1 – Relates to the previous guided – Relates to the previous guided program 3.3 Average.program 3.3 Average.

The user, at the moment, can enter one set of The user, at the moment, can enter one set of exam marks, find their average and then the exam marks, find their average and then the program ends. program ends.

Please change it so that the user can enter as Please change it so that the user can enter as many sets of marks as they wish. Add a reset many sets of marks as they wish. Add a reset button to begin the new set of marks. Display button to begin the new set of marks. Display only the current set of marks in the list box.only the current set of marks in the list box.

Basically meaning add a “Basically meaning add a “ResetReset” button which will ” button which will return return everythingeverything ((labelslabels, , buttonsbuttons & & global global variablesvariables)) to the way they were when you first ran the to the way they were when you first ran the program, so the user does not have to Quit and re-run program, so the user does not have to Quit and re-run the program to start again.the program to start again.

Page 28: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

282819/04/2319/04/23

Extension “Calculator” Program 2Extension “Calculator” Program 2

Build a simple calculator as shown.Build a simple calculator as shown.

Page 29: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

2929

Extension “Calculator” Program 2Extension “Calculator” Program 21.1. The user enters two numbers and then clicks one of the four The user enters two numbers and then clicks one of the four

arithmetic operators. arithmetic operators. These four arithmetic operators are These four arithmetic operators are disableddisabled until the user types in the until the user types in the

First NumberFirst Number text box. text box.2.2. The result is then displayed in the Result text box. The result is then displayed in the Result text box.

The The ResultResult label and text box are label and text box are invisibleinvisible until the user clicks an until the user clicks an operation button.operation button.

3.3. Clicking the Clear button shouldClicking the Clear button should Clear the Clear the FirstFirst number, number, SecondSecond number and number and ResultResult text boxes (e.g. text boxes (e.g.

txtFirstNumber.Text = “”)txtFirstNumber.Text = “”) Make the Make the ResultResult label and text box label and text box invisibleinvisible.. Disable the operation buttons again.Disable the operation buttons again.

4.4. M+M+ adds the result to memory. adds the result to memory.5.5. M-M- clears the memory (resets it to 0). clears the memory (resets it to 0).6.6. MRCMRC puts the number in memory into the puts the number in memory into the FirstFirst NumberNumber text box. text box.7.7. Write comments for each line code to explain what they do.Write comments for each line code to explain what they do.

• Note that you should use the same names for local variables like FirstNumber & Note that you should use the same names for local variables like FirstNumber & SecondNumber in each “calculation” button + - * /. Please include a comment on SecondNumber in each “calculation” button + - * /. Please include a comment on the declaration lines of these variables which describes why it is possible for the declaration lines of these variables which describes why it is possible for different procedures to use the same variable names and not cause a problem different procedures to use the same variable names and not cause a problem when the program is run.when the program is run.

Page 30: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

Extension “/, DIV or MOD” Program 3Extension “/, DIV or MOD” Program 3Raul writes software for a melon packing plant. Raul has Y melons which are to be packed into boxes. Each box contains X melons.

Write a program to accept:1. How many melons are to be packed.2. How many melons each box contains.

And then calculates:1. The number of boxes to 2 decimal places. 2. The number of full boxes.3. The number of melons left over.

Use each of “/ , DIV or MOD” and decide for yourself which one is suitable for each of the calculations above by trying each one (make sure you know what to expect though, before you entering your numbers).

For /

/ as normal. For DIV

Int (….. / …. ) as VB uses “INT” rather than “DIV”. For MOD

…. Mod …

Page 31: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

313119/04/2319/04/23

PlenaryPlenary

Explain the circumstances when the Explain the circumstances when the following might be useful:following might be useful: Disabling buttons and making text boxes & Disabling buttons and making text boxes &

labels invisible at design time;labels invisible at design time; Enabling buttons and making text boxes & Enabling buttons and making text boxes &

labels visible when particular events happen labels visible when particular events happen during run time. during run time.

When we want to restrict the user’s access to When we want to restrict the user’s access to these controls until the user has performed certain these controls until the user has performed certain events.events.

Page 32: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

323219/04/2319/04/23

PlenaryPlenary

Explain the TextChanged event.Explain the TextChanged event. Is activated when text in a control is changed Is activated when text in a control is changed

e.g. Something is typed into a TextBoxe.g. Something is typed into a TextBox

Page 33: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

333319/04/2319/04/23

PlenaryPlenary

Explain what the Round function is for and Explain what the Round function is for and how it is used.how it is used. Used for rounding numbers and currency.Used for rounding numbers and currency. Needs two arguments.Needs two arguments.

Variable or number to be formatted.Variable or number to be formatted.Number of decimal places to be rounded to.Number of decimal places to be rounded to.Math.Round(Math.Round(VariableOrNumberToBeRoundedVariableOrNumberToBeRounded, , NumberOfDecimalPlacesToBeRoundedToNumberOfDecimalPlacesToBeRoundedTo))

Page 34: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

343419/04/2319/04/23

PlenaryPlenary

Explain what the Random function is for Explain what the Random function is for and how it is used.and how it is used. Used for giving a random number between 0 Used for giving a random number between 0

and 1.and 1. To generate whole numbers between 2 specified To generate whole numbers between 2 specified

values:values:Int(Rnd() * Int(Rnd() * HigherValueHigherValue + + LowerValueLowerValue))

Page 35: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

353519/04/2319/04/23

PlenaryPlenary

What is the general form of the code which What is the general form of the code which will clear the text property of a control?will clear the text property of a control?

Page 36: 06/10/20151 3.3 Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and

363619/04/2319/04/23

Clearing the text property of a Clearing the text property of a controlcontrol

….Text = “”

Name of controlName of control