EXAMPLE 5: If the idea about this old fasioned menu excites you, Before
we get into the
more exciting world of graphics and see how to create modern menu's, let us write a
program which creates
a set of menues to show how to do most programming tasls in PC#
which do not involve graphics.
The program is large in size, but don't let this worry you. Most of
it is text to display
and you already know every thing necessary to understand it. So go through it, then read
the tutorial.
=========================================================================================
public
class a : pcs {
// ** VAR's Used:
// ** x=order of selection in first
menu y=order of selection in second menu
public override void init() {
tia=toa="t";
// Select "text screen for both text input & output
bli=1;
// Requesting to start execution at block 1.
base.init();
// Initializing PC# classes. Must be last statement
}
public override void run() {
// ------------------------------------
Main Menu ---------------------------------------
if (blp==1) {
// Main Menu Block
cls="r0";fns="trb14";tm("c"); // Set font,clor, clear screen.
os="
MAIN MENU";tm();
os="";tm();
// Display title
cls="b0";fns="trbi12";
// change to blue bold, italic size 12 text writing
os=" Demonstrates
how easy and efficient Personal C# programming can be!";
tm();
// Display sub-title.
os="";tm();
// Skip one line
cls="S9";fns="trb10";
// Change color, font
os=" (G) General tasks menu.";tm();
os=" (S) System operations menu.";tm();
os=" (F) Files and Directories menu.";tm();
os=" (N) Networking menu.";tm();
os=" (E) Exit Program.";tm();
os="";tm();
// skip one line
cls="b0";os="Selection :";bli=2;tm("i");return;
// Get user selection and jump to block 2.
}
// -------------------------- Main Menu Selection Processing-----------------------------
else if (blp==2) {
os=os.ToUpper();
// Convert selection to upper case
x="GSFNE".IndexOf(os);
// Get selection index
if (x<0){
// If unexpected char entered
bli=1;um("b");return;
// return to block 1.
}
// else jump to the block which executes selection
else if (x==0) {bli=3;um("b");return;}
else if (x==1) {bli=4;um("b");return;}
else if (x==2) {bli=5;um("b");return;}
else if (x==3) {bli=6;um("b");return;}
else if (x==4) sm("e");
// If selection was "E or e" exit program
}
// -------------------------------- General
Tasks Menu -----------------------------------
else if (blp==3) {
cls="r0";fns="trb14";tm("c");
os="
GENERAL TASKS MENU";tm();
os="";tm();
cls="S9";fns="trb10";
os=" (1) Get a random number.";tm();
os=" (2) Display my message in large size
letters.";tm();
os=" (3) Display a message into a dialog box.";tm();
os=" (E) Exit to Upper menu.";tm();
os="";tm();
cls="b0";os="Selection :";bli=9;tm("i");return;
// Get user selection and jump to block 9.
}
// ----------------------------- System Operations Menu
---------------------------------
else if (blp==4) {
cls="r0";fns="trb14";tm("c");
os="
SYSTEM OPERATIONS MENU";tm();
os="";tm();
cls="S9";fns="trb10";
os=" (1) Display all environment names and their values.";tm();
os=" (2) Display
user's domain name, computer name and user name.";tm();
os=" (3) Display local drives
of this computer.";tm();
os=" (4) Get version number of both C# and Operating System.";tm();
os=" (5) Display current Date and Time.";tm();
os=" (E) Exit to upper menu.";tm();
os="";tm();
cls="b0";os="Selection :";bli=9;tm("i");return;
// Get user selection and jump to block 9.
}
// ---------------------------- Files & Directories
Menu --------------------------------
else if (blp==5) {
cls="r0";fns="trb14";tm("c");
os="
FILES AND DIRECTORIES MENU";tm();
os="";tm();
cls="S9";fns="trb10";
os=" (1) Get the name of current directory.";tm();
os=" (2) Create a file.";tm();
os=" (3) Delete a file.";tm();
os=" (4) Tell me if it is a file, directory or
non-existing.";tm();
os=" (5) List all sub-directories and files in current directory.";tm();
os=" (6) Copy a file to another.";tm();
os=" (E) Exit to upper menu.";tm();
os="";tm();
cls="b0";os="Selection :";bli=9;tm("i");return;
// Get user selection and jump to block 9.
}
// -------------------------------- Networking Menu
------------------------------------
else if (blp==6) {
cls="r0";fns="trb14";tm("c");
os="
NETWORKING MENU";tm();
os="";tm();
cls="S9";fns="trb10";
os=" (1) Get a web page from the internet and display its contents.";tm();
os="
(E) Exit to Upper menu.";tm();
os="";tm();
cls="b0";os="Selection
:";bli=9;tm("i");return;
// Get user selection and jump to block 9.
}
// ------------------------- Sub-Menues Selection Processing
----------------------------
else if (blp==9) {
os=os.ToUpper();
// Convert selection to upper case
y="123456E".IndexOf(os);
// Get selection index
if (y==6) {
// If "Exit to upper menu" selected
bli=1;um("b");return;
// return to main menu block
}
// Else, if unexpected char or larger than expected
// number for the particular sub-menu was entered
else if (y<0 || (x==0 && y>2)
|| (x==1 && y>4) || (x==2 && y>5) || (x==3 && y>0)) {
bli=x+3;um("b");return; // return to the same sub-menu.
}
else {
bli=x*2+10;um("b");return;
// else jump to the block which executes selection
}
}
// -----------------------
Execution of Sub-Menue G Selections --------------------------
else if (blp==10) {
// Data Collection Block
if (y==0){
// Sel 1: Random number
os="Enter the highest integer value, the random number
can be:";
bli=11;tm("i");return; // Get
data from user and jump to execution block
}
else if (y==1
|| y==2){ // Sel 2,3: Msg to be displayed
os="Enter message to be displayed: ";bli=11;tm("i");return;
}
}
else if (blp==11) {
// Execution Block
if (y==0){
// Sel 1: Random number
i=Int32.Parse(os);um("mr"); // Convert to int
and call um("mr) with it
os="Random Number: "+o;tm(); // Display Random number
obtained
}
else if (y==1){
// Sel 2: Banner
fns="trb48";cls="p0";tm(); // Change Font to large
size, display msg
fns="trb10";cls="S9";
// return font & color to their original values
}
else if (y==2){ // Sel3: dialog
box display
cm("d");
// Display returned string in (os) into a dialog box
}
os="";tm();cls="r0"; // After selection has been executed,Skip
one line
// then display the following instruction in red:
os="Hit [ENTER] to return to menu.";bli=x+3;tm("i");return;
// then return to the same sub-menu
}
// ----------------------- Execution of Sub-Menue S Selections
--------------------------
else if (blp==12) {
// Data Collection Block
bli=13;um("b");return;
// Go to execution block direcly since no data
// is required by any selection in this menu.
}
else if (blp==13) {
// Execution Block
if (y==0){
// Sel 1: Display environment var's
sm("gE");
// Get all env keys in KS[] and values in OS[]
for (c=0;c<KS.Length;c++)
{ // Scan the 2 arrays,
if (KS[c].Length<1) continue; // Skip no data rows.
cls="b0";os="KEY: "+KS[c];tm(); // Display each key in blue
cls="S9";os="VALUE: "+OS[c];tm(); // and its value in black.
os="";tm();
// Skip one line for seperation.
}
}
else if (y==1){
// Sel 2: Get System/User info
sm("gd");os="User's Domain: "+os;tm();// Get
& Display Usr's Domain
sm("gc");os="Computer name: "+os;tm();// Get &
Display Computer name
sm("gu");os="User name: "+os;tm(); //
Get & Display User name
}
else if (y==2){
// Sel 3: Get logical drives
sm("gl");
// Get drive names in OS[]
om("fa");tm();
// Convert OS[] to (os) and display.
}
else if (y==3){
// Sel 4: Get Version numbers
sm("gv");os="C# Version number: "+os;tm();
sm("go");os="Operating Systm Version: "+os;tm();
}
else
if (y==4){
// Sel 5: Get date/time
sm("dl");os="Current Date: "+os;tm(); // Get long format
of date & display it.
sm("dt");os="Current Time: "+os;tm(); // Get time
& display it.
}
os="";tm();cls="r0";
// At the end return to the same sub-menu
os="Hit [ENTER] to return to menu.";bli=x+3;tm("i");return;
}
// ----------------------- Execution of Sub-Menue F Selections --------------------------
else
if (blp==14) { // Data Collection Block
if (y==0) {
// Sel 1: Get Current dir
bli=15;um("b");return;
// Go to Execution block directly.
}
else if (y==1 ||
y==2){ // Sel 2,3: Create/Delete a file
os="Enter file name (Remember to double backlashes): ";
bli=15;tm("i");return;
// Get data from user and jump to execution block
}
else
if (y==3){ // Sel 4: Check attributes
os="Enter file or directory name (Remember to double backlashes): ";
bli=15;tm("i");return;
}
else if (y==4){
// Sel 5: Listing folder contents.
os="Enter directory name (Remember to double
backlashes): ";
bli=15;tm("i");return;
}
else if (y==5){ // Sel 6: Copy
os="Enter source, destination files seperated with a comma: ";bli=15;tm("i");return;
}
}
else if (blp==15) {
// Execution Block
if (y==0){
// Sel 1: Get current dir
fm(".");
// Get directory name in (os) and display it.
os="Current Directory: "+os;tm();
}
else if (y==1){
// Sel 2: Create file
fls=os;ks="f";fm("M");
// Create the file
os="File has been created.";tm();
}
else if (y==2){
// Sel 3: Delete file
fls=os;ks="f";fm("D");
// Delete the file
os="File has been deleted.";tm();
}
else if (y==3){
// Sel 4: Check attributes
fls=os;fm("A");
// Get file attributes.
n="fd ".IndexOf(os);
// Check to see if returned string is "f", "d" or " "
if (n==0) os="a file";
else if (n==1) os="a directory"; else os="non existing";
os="It was found to
be "+os;tm();
// Display result.
}
else if (y==4){
// Sel 5: Display folder contents
fls=os;ks="d";fm("L");
// Get all sub-dir's in OS[]
om("fa");
// Convert OS[] to (os)
ns=os;
// save (os) temporarely
ks="f";fm("L");
// Get all files in OS[]
om("fa");
// Convert to (os)
os=ns+os;tm();
// Add the 2 lists & display
}
else if (y==5){
// Sel 6: Copy a file
oc=',';om("s");
// Seperate the 2 files names recieved into OS[]
fls=OS[0];os=OS[1];fm("C");
// Copy source file to dest file
os="File copied";tm();
// Inform user
}
os="";tm();cls="r0";
// At the end return to the same sub-menu
os="Hit [ENTER] to return to menu.";bli=x+3;tm("i");return;
}
// ----------------------- Execution of Sub-Menue N Selections --------------------------
else
if (blp==16) { // Data Collection Block
cls="r0";os="Please know that retrieving your web page may take a long time.";tm();
cls="b0";os="Enter
web page address: ";
bli=17;tm("i");return;
// Get data from user and jump to execution block
}
else if (blp==17) {
// Execution Block
urs=os;
// Assign received string to (urs), restore color
nm("hg");tm();
// Get the web page and display its contents
os="";tm();cls="r0";
// At the end return to the same sub-menu
os="Hit [ENTER] to return to menu.";bli=x+3;tm("i");return;
}
}
}
=========================================================================================
HOW TO
WRITE, COMPILE AND RUN THE PROGRAM? See Example 1.
=========================================================================================
TUTORIAL:
Program
design is simple. Because it is easier to work with numbers than with strings,
we have used the orders of selected items
in each menu to identify the selection. We used
(x) to store main menu selection and (y) to store the sub-menu selection.
x and y are two
variables of int type which you don't have to declare since
PS# has done this job for you. See PS# Referance for a list of variables of this kind.
Every thing else in this program is self explanatory.
=========================================================================================
Output
of Example 5 is shown below: