Q1. Create a project which contains a form that can be used as a ‘login’ form to an application. Place a label, a text box, and a command button on the form. Ensure that the text box receives focus on startup. Place code in the Click event of the command button, so that the entry in the text box is validated against a password you have selected (use a simple If statement). If the password is correct, then display a message box congratulating the user on their correct choice. Use an Else statement to display a message box warning the user that they have entered an incorrect password.
Q3. Write an appropriate If-Then-Else block for the following situation: Test the string variable Flag. If Flag=’True’, set count equal to 0 and assign the message “Resetting the Counter” to the string variable Msg1. Then test the value of the singleprecision real variable Z.
Private sub Form_Load() Form1.Show Text1.SetFocus End Sub
If Flag = “False”, increase the value of count by 1, then test the string variable Type If Type equals “A”, add the value of U to Z If Type equals “B” add the value of V to Z Otherwise, add the value of W to Z Finally, reset the value of flag to “True”
Private Sub Command_Click() If Text1.Text = “Password” then MsgBox “ Congratulations, this is correct” Else MsgBox “Sorry, the password is not correct” Text1.Text =” “ Text1.SetFocus End if End Sub
Q2. Create a project with a form and a single command button. Write code so that if the command button is clicked, a running total of the number of times the command button has been clicked is maintained and displayed. Either declare a module level variable, or to declare a Static type variable Private Sub Command_Click() Static intX as Integer intX = intX +1 MsgBox “This button has been clicked “ & intX & “Times “ End Sub
If Z exceeds Zmax, assign the message “Maximum Value Exceeded” to the string variable Msg2, and assign the value of Zmin to Z. Otherwise, add the value of Zmin to Z.
If Flag = "True" Then Count = 0 Msg1 = "resetting the Counter" If Z > Zmax Then Msg2 = "Maximum Value Exceeded" Z = Zmin Else Z=Z+W End If Else Count = Count + 1 If Type = "A" Then Z=Z+U Elseif Type = "B" Then Z=Z+V Else Z=Z+W End If Flag = "true" End If
Project A: Convert temperatures
Project B: Accumulate test score data
Operation
Operation
•
When the user clicks on an option button to determine the type of conversion to be done, the application clears both text boxes and locks the one that the result will be displayed in.
• The user enters test scores one at a time and then clicks the Enter score button.
•
When the user enters a valid numeric entry and clicks on the Calculate button, the application displays the result in the locked text box.
•
When the user enters an invalid entry, the application displays an appropriate error message in a message box. When the user responds to the message, the focus returns to the text box.
• For each entered score, the application adds one to the number of scores, calculates the average score, and determines what the best score is so far. Then, it displays the number of scores, average score, and best score in the three label controls. Specifications
Specifications
• The average score is the sum of all scores divided by the number of scores.
•
The formula for converting the temperature from Fahrenheit to Celsius is (F-32)*5/9.
•
The formula for converting the temperature from Celsius to Fahrenheit is (C*9/5)+32.
•
Write the code so the application is as quick and easy to use as possible. For example, the Calculate button should be activated when the user presses the Enter key, and the focus should be moved to the appropriate control when the user clicks on an option button or presses the Tab key.
• Use labels, not text boxes, to display the number of scores, average score, and best score, but set the BorderStyle property to Fixed Single so the labels look like those above.
•
Write the code so it checks all entries for validity and catches any other errors without blowing up.
Option Explicit Private Sub cmdCalculate_Click() If optFahrToCelsius Then txtCelsius = (txtFahrenheit - 32) * 5 / 9 Else txtFahrenheit = (txtCelsius * 9) / 5 + 32 End If End Sub Private Sub optCelsiusToFahr_Click() txtFahrenheit.Locked = True txtFahrenheit.TabStop = False txtCelsius.Locked = False txtCelsius.TabStop = True ClearControls End Sub Private Sub optFahrToCelsius_Click() txtFahrenheit.Locked = False txtFahrenheit.TabStop = True txtCelsius.Locked = True txtCelsius.TabStop = False ClearControls End Sub Private Sub ClearControls() txtFahrenheit = "" txtCelsius = "" End Sub Private Sub cmdExit_Click() Unload Me End Sub
• Write the code so the application is as quick and easy to use as possible. For example, the Enter score button should be activated when the user presses the Enter key, and the Test score box should be cleared after the calculations are done. • Write the code so it checks all entries for validity and catches any other errors without blowing up. Enhancements • Add a Reset button that resets all of the text boxes so the user can start entering the scores for another test. Option Explicit Dim iNumberOfScores As Integer Dim iBestScore As Integer Dim iTotalScores As Integer Private Sub cmdEnterScore_Click() iNumberOfScores = iNumberOfScores + 1 iTotalScores = iTotalScores + txtTestScore If txtTestScore > iBestScore Then iBestScore = txtTestScore lblNumScoresValue = iNumberOfScores lblAvgScoreValue = iTotalScores / iNumberOfScores lblBestScoreValue = iBestScore txtTestScore = "" txtTestScore.SetFocus End Sub Private Sub cmdExit_Click() Unload Me End Sub