![]() |
![]() |
![]() |
|
|
||
|
EXTRA INFO |
Excellent !! lesson 8(ii) - AIM - To Understand Msg and Input Boxes More fully. |
|
|
As well as adding extra buttons to a message box we can also give it a title. Look again at the msgbox prompt below
If I want the message box to have Yes, No and Cancel buttons I would code as below. AFTER the type of buttons and icons I would place a comma and then the Title of the msgBox (what appears in the title bar) in speech marks. Choice = msgbox("Would you like to play my guessing game?, vbOkYesCancel, "Play Game?") This now creates a third option for our if statement. So far we have only had two. We now progress to - If...Then...ElseIf...Then...Else...End If The user has a choice of 3 buttons, Yes, No or Cancel. Try to write the code that will tell the user what button they clicked.
SPOILER WARNING CODE BELOW
If Choice =6 Then msgbox "You clicked Yes." ElseIf Choice=7 Then msgbox "You clicked No" Else msgbox "You clicked Cancel End If This gives the program 3 options, with only one end If. The use of the ElseIf allows the choice to re-start the decision statement.
Note that this gives the message boxes a title. Play Game? shown here. The corresponding Yes, No and Cancel message boxes all have titles. Providing there are no brackets used in the msgBox code and an empty space is left for the type of message box, this will work fine. Don't forget to comment and indent your code. There are also some codes we can use for Inputbox, these extra arguments will give the Input box a title AND give a default text entry. Lets write the code to get the user to enter a Y, N or C into an Inputbox representing Yes, No and Cancel respectively. Then display the same messages as above back to the user. Use the code above to help and write the program. Don't forget to change the variable data type to String!
You can see that what is written on the message box is the first part of the InputBox statement. The second part after a comma gives us the heading and the final part after another comma gives us the default text. We would also need to plan and test the program. The code needs to check that the user has indeed input a Y, N or C as this code will display the "You entered C" message box for any value other than N or Y. As well as this our user may input a lowercase y, n or c so the program would need to convert the entry to uppercase. The code for this is Choice=UCase(Choice) This ensures that Choice holds an uppercase value. The opposite code LCase(Choice) would convert to lowercase. Complete your plan, code test and run this program. Now create a 'game' that allows the user to 'communicate with the computer' by means of input and message boxes. Use If...Then....ElseIf...Then....Else....End If statements to increase the options presented to the user. Plan, Code and Test your program. Once your program is working perfectly, save and then save as an .exe file so that youcan play your game as a 'stand alone' application away from the workspace. To save as a .exe file go File>>Make YourFileName.exe...
And save the program to a suitable folder. Then close VB open the folder and double click on the Program name. exe icon
The program will run as a standalone application! Play it with your friends and see what they did too.
Well done that completes this VB6 course. We hope you enjoyed the course and found it useful. Please forward any feedback to lbcwood@hotmail.com Don't forget to check you have read all the extra Info in the links on the left.
|
||