Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 06/10/20151 3.3 Working with Data

2 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and making text boxes & labels invisible at design time; Disabling buttons and making text boxes & labels invisible at design time; Enabling buttons and making text boxes & labels visible when particular events happen during run time. Enabling buttons and making text boxes & labels visible when particular events happen during run time.

3 306/10/2015 Learning Objectives Explain the TextChanged event. Explain what the Round and Random functions are for and how they are used. State the general form of the code which will clear the text property of a control.

4 406/10/2015 Constants 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.25 If you try to assign another value to a constant later in the program an error will occur.

5 506/10/2015 Constants 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 programmer would have to go through and change the value every time it occurs. 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 occurs. It makes the code more meaningful to anybody reading it. It makes the code more meaningful to anybody reading it.

6 Arithmetic & Order of Precedence Operator Visual Basic Order of precedence Exponentiation (e.g. 10 2 = 10^2 ) ^1 Negation-2 Multiplication*3 Division/3 Integer Division (produces a whole number by removing any decimal parts) \4 Modulus (gives a remainder after division e.g.12 Mod 5 = 2) Mod5 Addition+6 Subtraction-6

7 706/10/2015 Clearing the text property of a control …. Text = “” Name of control

8 806/10/2015 Program 3.3 Average Specification: Allow the user to enter as many exam marks as they wish and display them in a list box. Allow the user to enter as many exam marks as they wish and display them in a list box. The program finishes when the user clicks a button to show the average mark rounded to 2 decimal places. The program finishes when the user clicks a button to show the average mark rounded to 2 decimal places. Allow the user to use automatically generated random marks. Allow the user to use automatically generated random marks.

9 906/10/2015 Program 3.3 Average Create a new project named ‘Average’. Change the form’s Text property to ‘Calculating the average exam mark’.

10 1006/10/2015 Program 3.3 Average Place 3 Labels, 1 TextBox, 4 Buttons and 1 ListBox on the form (and follow the instructions on the next slide) as shown below.

11 11 Properties to set: Label1: Name: lblMark Name: lblMark Text: Enter exam mark Text: Enter exam mark Bold: True Bold: True Size: 12 Size: 12Label2: Name: lblMean Name: lblMean Text: Mean exam mark Text: Mean exam mark Bold: True Bold: True Size: 12 Size: 12 Visible: False Visible: FalseLabel3: Name: lblMeanResult Name: lblMeanResult Size: 12 Size: 12 Visible: False Visible: FalseTextBox1: Name: txtMark Name: txtMark Size: 12 Size: 12 Button1: Name: butOK Text: OK Size: 12 Bold: True Enabled: False Button2: Name: butMean Text: Show Mean Size: 12 Bold: True Enabled: False Button3: Name: butQuit Text: Quit Size: 12 Bold: True Button4: Name: butRandom Name: butRandom Text: Random Mark Text: Random Mark Size: 12 Size: 12 Bold: True Bold: True Enabled: False Enabled: FalseListBox1 Name: lstMarks Name: lstMarks Size: 12 Size: 12

12 1206/10/2015 Program 3.3 Average The reason for disabling the butOK is that the user should not be able to click it until he/she has entered a mark. The reason for making lblMean & lblMeanResult invisible is that they are useless until he/she has entered a mark.

13 Program 3.3 Average Two variables ‘Total’ & ‘NumberOfMarks’ will be used by the butOK & butMean procedures so declare them globally outside these procedures (e.g. Double click the OK button, move up out of its procedure and create a blank line in the Form declarations area): Dim Total As Integer ‘Declares a variable to store running total of marks. It 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 is running and its value “remembered” between clicks of either button. Dim Total As Integer ‘Declares a variable to store running total of marks. It 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 is running and its value “remembered” between clicks of either button. Dim NumberOfMarks As Integer ‘Declares a variable to store the number of 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 to exist while the program is running and its value “remembered” between clicks of either button. Dim NumberOfMarks As Integer ‘Declares a variable to store the number of 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 to exist while the program is running and its value “remembered” between clicks of either button. See next slide for a screenshot.

14 1406/10/2015 Program 3.3 Average

15 1506/10/2015 Program 3.3 Average When the OK button is clicked: The butMean button must be enabled so the user can find the mean. The butMean button must be enabled so the user can find the mean. The exam mark entered by the user must be removed and the cursor positioned in it ready for another mark. The exam mark entered by the user must be removed and the cursor positioned in it ready for another mark.

16 1606/10/2015 Program 3.3 Average Move back inside the butOK procedure and enter the 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

17 1706/10/2015 Program 3.3 Average Still inside the butOK procedure and enter the following 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.

18 1806/10/2015 Program 3.3 Average Go back to design view and double click the Show Mean button and enter the following code in its procedure 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

19 1906/10/2015 Program 3.3 Average Still in the Show Mean button and enter the following code in its procedure 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.

20 2006/10/2015 Program 3.3 Average Go back to design view and double click the Quit button and enter the following code in its proceduretemplate: Go back to design view and double click the Quit button and enter the following code in its procedure template: Application.Exit() ‘Exit the application. Application.Exit() ‘Exit the application. Save and run the program. What is the problem? The OK button is disabled so the mark cannot be processed! The OK button is disabled so the mark cannot be processed! We disabled it at design time so that the user cannot click it before a mark is entered. We disabled it at design time so that the user cannot 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.

21 2106/10/2015 Program 3.3 Average We will use the TextChanged event of txtMark. i.e. the OK button will be enabled when the contents of txtMark changes i.e. the OK button will be enabled when the contents of txtMark changes Double click the txtMark. Notice VB has correctly chosen the TextChanged event. Notice VB has correctly chosen the TextChanged event. Enter the following code in its proceduretemplate: Enter the following code in its procedure template: butOK.Enabled = True Double click the butOK. butMean.Enabled = True

22 2206/10/2015 Round function Used for rounding numbers and currency. 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: Math.Round( VariableOrNumberToBeRounded, NumberOfDecimalPlacesToBeRoundedTo )

23 2306/10/2015 Program 3.3 Average Double click the Random button and change the display line to. Double click the Random button and change the display line to. txtMark.Text = Int(Rnd() * 100 + 0) ' Generate a random value between 0 and 100. txtMark.Text = Int(Rnd() * 100 + 0) ' Generate a random value between 0 and 100.

24 2406/10/2015 Random function Gives a random number between 0 and 1. Written as Rnd() in VB. To generate whole numbers between 2 specified values: Int(Rnd() * HigherValue + LowerValue) Int(Rnd() * HigherValue + LowerValue)e.g. ' To generate random value between 1 and 6. ' To generate random value between 1 and 6. Int(Int((Rnd())*6 + 1))

25 2506/10/2015 Program 3.3 Average Double click the Show Mean button and change the display line to. Double click the Show Mean button and change the display line to. lblMeanResult.Text = Math.Round(Mean, 2) ‘Display the variable Mean formatted to 2 decimal places. lblMeanResult.Text = Math.Round(Mean, 2) ‘Display the variable Mean formatted to 2 decimal places.

26 Writing code which is easy to understand: Sensible variable names. Keywords in capitals. So that the reader does not have to keep cross referencing with a table of variable names. So that the reader does not have to keep cross referencing with a table of variable names. Comments or Annotation. To explain the logic of the code. To explain the logic of the code. Indentation (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: Private Sub butCalculateMean … Private Sub butCalculateMean … Dim …. Mean = Total / NumberOfMarks lblMeanResult.Text = Mean. ….. End Sub End Sub 26

27 2706/10/2015 Extension 1 – Relates to the previous guided program 3.3 Average. The user, at the moment, can enter one set of exam marks, find their average and then the program ends. The user, at the moment, can enter one set of exam marks, find their average and then the program ends. Please change it so that the user can enter as many sets of marks as they wish. Add a reset button to begin the new set of marks. Display only the current set of marks in the list box. Please change it so that the user can enter as many sets of marks as they wish. Add a reset button to begin the new set of marks. Display only the current set of marks in the list box. Basically meaning add a “Reset” button which will return everything (labels, buttons & global variables) to the way they were when you first ran the program, so the user does not have to Quit and re-run the program to start again. Basically meaning add a “Reset” button which will return everything (labels, buttons & global variables) to the way they were when you first ran the program, so the user does not have to Quit and re-run the program to start again.

28 2806/10/2015 Extension “Calculator” Program 2 Build a simple calculator as shown.

29 29 Extension “Calculator” Program 2 1.The user enters two numbers and then clicks one of the four arithmetic operators. These four arithmetic operators are disabled until the user types in the First Number text box. These four arithmetic operators are disabled until the user types in the First Number text box. 2.The result is then displayed in the Result text box. The Result label and text box are invisible until the user clicks an operation button. The Result label and text box are invisible until the user clicks an operation button. 3.Clicking the Clear button should Clear the First number, Second number and Result text boxes (e.g. txtFirstNumber.Text = “”) Clear the First number, Second number and Result text boxes (e.g. txtFirstNumber.Text = “”) Make the Result label and text box invisible. Make the Result label and text box invisible. Disable the operation buttons again. Disable the operation buttons again. 4.M+ adds the result to memory. 5.M- clears the memory (resets it to 0). 6.MRC puts the number in memory into the First Number text box. 7.Write comments for each line code to explain what they do. Note that you should use the same names for local variables like FirstNumber & SecondNumber in each “calculation” button + - * /. Please include a comment on 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 when the program is run. Note that you should use the same names for local variables like FirstNumber & SecondNumber in each “calculation” button + - * /. Please include a comment on 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 when the program is run.

30 Extension “/, DIV or MOD” Program 3 Raul 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. 1.How many melons are to be packed. 2. 2.How many melons each box contains. And then calculates: 1. 1.The number of boxes to 2 decimal places. 2. 2.The number of full boxes. 3. 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 …

31 3106/10/2015 Plenary Explain the circumstances when the following might be useful: Disabling buttons and making text boxes & labels invisible at design time; Disabling buttons and making text boxes & labels invisible at design time; Enabling buttons and making text boxes & labels visible when particular events happen during run time. Enabling buttons and making text boxes & labels visible when particular events happen during run time. When we want to restrict the user’s access to these controls until the user has performed certain events.

32 3206/10/2015 Plenary Explain the TextChanged event. Is activated when text in a control is changed e.g. Something is typed into a TextBox Is activated when text in a control is changed e.g. Something is typed into a TextBox

33 3306/10/2015 Plenary Explain what the Round function is for and 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. Number of decimal places to be rounded to. Math.Round( VariableOrNumberToBeRounded, NumberOfDecimalPlacesToBeRoundedTo )

34 3406/10/2015 Plenary Explain what the Random function is for and how it is used. Used for giving a random number between 0 and 1. Used for giving a random number between 0 and 1. To generate whole numbers between 2 specified values: To generate whole numbers between 2 specified values: Int(Rnd() * HigherValue + LowerValue)

35 3506/10/2015 Plenary What is the general form of the code which will clear the text property of a control?

36 3606/10/2015 Clearing the text property of a control …. Text = “” Name of control


Download ppt "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."

Similar presentations


Ads by Google