Вы находитесь на странице: 1из 2

Q1. Q3.

Create a project which contains a form that can be used as a ‘login’ form to an application. Write an appropriate If-Then-Else block for the following situation:
Place a label, a text box, and a command button on the form. Ensure that the text box Test the string variable Flag. If Flag=’True’, set count equal to 0 and assign the message
receives focus on startup. “Resetting the Counter” to the string variable Msg1. Then test the value of the single-
Place code in the Click event of the command button, so that the entry in the text box is precision real variable Z.
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 If Z exceeds Zmax, assign the message “Maximum Value Exceeded” to the string
correct choice. Use an Else statement to display a message box warning the user that they variable Msg2, and assign the value of Zmin to Z.
have entered an incorrect password. Otherwise, add the value of Zmin to Z.

Private sub Form_Load() If Flag = “False”, increase the value of count by 1, then test the string variable Type
Form1.Show If Type equals “A”, add the value of U to Z
Text1.SetFocus If Type equals “B” add the value of V to Z
End Sub 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” If Flag = "True" Then
Else Count = 0
MsgBox “Sorry, the password is not correct” Msg1 = "resetting the Counter"
Text1.Text =” “
Text1.SetFocus If Z > Zmax Then
End if Msg2 = "Maximum Value Exceeded"
End Sub Z = Zmin
Else
Z=Z+W
End If
Else
Q2. Count = Count + 1
Create a project with a form and a single command button. Write code so that if the If Type = "A" Then
command button is clicked, a running total of the number of times the command button has Z=Z+U
been clicked is maintained and displayed. Elseif Type = "B" Then
Z=Z+V
Else
Either declare a module level variable, or to declare a Static type variable Z=Z+W
End If

Private Sub Command_Click() Flag = "true"


Static intX as Integer End If
intX = intX +1
MsgBox “This button has been clicked “ & intX & “Times “
End Sub
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 • The user enters test scores one at a time and then clicks the Enter score button.
application clears both text boxes and locks the one that the result will be displayed in.

• For each entered score, the application adds one to the number of scores, calculates the
When the user enters a valid numeric entry and clicks on the Calculate button, the application displays
the result in the locked text box. 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.
• 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. 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. • Use labels, not text boxes, to display the number of scores, average score, and best
• The formula for converting the temperature from Celsius to Fahrenheit is (C*9/5)+32. score, but set the BorderStyle property to Fixed Single so the labels look like those
• Write the code so the application is as quick and easy to use as possible. For example, the Calculate above.
button should be activated when the user presses the Enter key, and the focus should be moved to the • Write the code so the application is as quick and easy to use as possible. For example,
appropriate control when the user clicks on an option button or presses the Tab key.
the Enter score button should be activated when the user presses the Enter key, and the
• Write the code so it checks all entries for validity and catches any other errors without blowing up. 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
Option Explicit
blowing up.
Private Sub cmdCalculate_Click()
If optFahrToCelsius Then Enhancements
txtCelsius = (txtFahrenheit - 32) * 5 / 9
Else • Add a Reset button that resets all of the text boxes so the user can start entering the
txtFahrenheit = (txtCelsius * 9) / 5 + 32 scores for another test.
End If
End Sub
Option Explicit
Private Sub optCelsiusToFahr_Click()
txtFahrenheit.Locked = True Dim iNumberOfScores As Integer
txtFahrenheit.TabStop = False Dim iBestScore As Integer
txtCelsius.Locked = False Dim iTotalScores As Integer
txtCelsius.TabStop = True
ClearControls
End Sub Private Sub cmdEnterScore_Click()
iNumberOfScores = iNumberOfScores + 1
Private Sub optFahrToCelsius_Click() iTotalScores = iTotalScores + txtTestScore
txtFahrenheit.Locked = False If txtTestScore > iBestScore Then iBestScore = txtTestScore
txtFahrenheit.TabStop = True
txtCelsius.Locked = True
lblNumScoresValue = iNumberOfScores
txtCelsius.TabStop = False
ClearControls lblAvgScoreValue = iTotalScores / iNumberOfScores
End Sub lblBestScoreValue = iBestScore

Private Sub ClearControls() txtTestScore = ""


txtFahrenheit = "" txtTestScore.SetFocus
txtCelsius = ""
End Sub
End Sub
Private Sub cmdExit_Click()
Unload Me Private Sub cmdExit_Click()
End Sub Unload Me
End Sub

Вам также может понравиться