Re: Dual mode fruit basket program in JScript .NET

This is instructive -- thanks.
--le

----- Original Message ----- 
From: "Jamal Mazrui" <empower@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, February 10, 2008 9:03 AM
Subject: Dual mode fruit basket program in JScript .NET


From the archive at
http://www.EmpowermentZone.com/dm_fruit.zip

This fruit basket program is writtin in JScript .NET, which is Microsoft
JavaScript built on the .NET Framework.  The language is documented at
http://msdn2.microsoft.com/en-us/library/72bd815a(VS.80).aspx

The program illustrates the dynamic nature of this language (though not as
dynamic as Ruby, Boo, or Python).  It also illustrates how a GUI can be
controlled in a console window that responds interactively to text-based
commands.  There are two modes of operation:  (1) a GUI dialog in a window
titled "Fruit Basket GUI," and (2) a command prompt in a window titled
"Fruit Basket Prompt."  You can operate the fruit basket in either mode by
Alt+Tabbing between them.

The GUI mode works like other fruit basket programs I have written.  Let
me thus demonstrate the command mode with the following session I
captured.  Each interaction starts with a comment in square brackets,
followed by the line of input preceded by the > prompt, and then the
resulting output.  That output is the result of the JScript eval function
converted to a string.  The output is "undefined" if there is no return
type.

[First, here is the title of the command mode window and its initial text]
Fruit Basket Prompt
Enter quit when done
>

[Enter a variable name to check its value.
The word "this" refers to the instance of the form]
> this
FruitBasket, Text: Fruit Basket GUI

[Similarly, show the result of the ToString() method
on the TableLayoutPanel object]
> tlp
System.Windows.Forms.TableLayoutPanel, BorderStyle:
System.Windows.Forms.BorderStyle.None

[Show the controls contained in tlp]
> for (o in tlp.Controls) print(o)
System.Windows.Forms.Label, Text: &Fruit:
System.Windows.Forms.TextBox, Text:
System.Windows.Forms.Button, Text: &Add
System.Windows.Forms.Label, Text: &Basket:
System.Windows.Forms.ListBox
System.Windows.Forms.Button, Text: &Delete

[Manually add a fruit to the basket, starting by putting text in the edit
box]
> txtFruit.Text = "apple"
apple

[Perform a click on the add button]
> btnAdd.PerformClick()
undefined

[Check that it got added to the basket]
> for (o in lstBasket.Items) print(o)
apple

[Add two fruit to the basket directly in a single command]
> lstBasket.Items.AddRange(["banana", "cherry"])
undefined

[Show the basket]
> for (o in lstBasket.Items) print(o)
apple
banana
cherry

[Change the caption of the delete button]
> btnDelete.Text = "&Remove"
&Remove

[Show the controls]
> for (o in tlp.Controls) print(o)
System.Windows.Forms.Label, Text: &Fruit:
System.Windows.Forms.TextBox, Text:
System.Windows.Forms.Button, Text: &Add
System.Windows.Forms.Label, Text: &Basket:
System.Windows.Forms.ListBox, Items.Count: 3, Items[0]: apple
System.Windows.Forms.Button, Text: &Remove

[Remove the selected item from the basket]
> btnDelete.PerformClick()
undefined

[Show the basket]
> for (o in lstBasket.Items) print(o)
banana
cherry

I found that a limitation of the GUI event processing is that Alt+F4 does
not work to close that window.  I think a custom button could do this, but
for now, enter the quit command in the console window.

Below is the source code of DualMode.js.  The batch file, build.bat,
produces the executable, DualMode.exe, which is about 11K in size.  No
other files are needed to run the program -- as long as .NET 2.0 or above
is installed.

Jamal

/*
content of DualMode.js
Fruit Basket program in JSscript .NET
Public domain by Jamal Mazrui
*/

import Accessibility
import System
import System.Drawing
import System.Windows.Forms

class FruitBasket extends Form {
var lblFruit, txtFruit, btnAdd, lblBasket, lstBasket, btnDelete, tlp

function onShown(o : Object, e : EventArgs) {
Console.Title = "Fruit Basket Prompt"
print("Enter quit when done")
Console.Write("> ")
while (true) {
while (!Console.KeyAvailable) Application.DoEvents()
var sInput = Console.ReadLine().Trim()
if (sInput == "quit") break
try {
var s = eval(sInput).ToString().Replace("Microsoft.JScript.Completion",
"")
print(s)
}
catch(ex) {
print(ex.Message)
}
Console.Write("> ")
} // while
Application.Exit()
} // onShown event handler

function onAdd(o : Object, e : EventArgs) {
var sFruit = txtFruit.Text.Trim()
if (sFruit.Length == 0) MessageBox.Show("No fruit to add!", "Alert")
else {
lstBasket.Items.Add(sFruit)
var iFruit = lstBasket.Items.Count - 1
lstBasket.SelectedIndex = iFruit
txtFruit.Clear()
}
} // onAdd event handler

function onDelete(o : Object, e : EventArgs) {
var iFruit = lstBasket.SelectedIndex
if (iFruit == -1) MessageBox.Show("No fruit to delete!", "Alert")
else {
lstBasket.Items.RemoveAt(iFruit)
if (iFruit == lstBasket.Items.Count) iFruit--
lstBasket.SelectedIndex = iFruit
}
} // onDelete event handler

function FruitBasket() {
lblFruit = new Label()
lblFruit.Text = "&Fruit:"
txtFruit = new TextBox()
btnAdd = new Button()
btnAdd.Text = "&Add"
btnAdd.add_Click(onAdd)
lblBasket = new Label()
lblBasket.Text = "&Basket:"
lstBasket = new ListBox()
btnDelete = new Button()
btnDelete.Text = "&Delete"
btnDelete.add_Click(onDelete)
tlp = new TableLayoutPanel()
tlp.Columns = 3
tlp.Rows = 2
tlp.Controls.AddRange([lblFruit, txtFruit, btnAdd, lblBasket, lstBasket,
btnDelete])
this.Controls.Add(tlp)
this.Text = "Fruit Basket GUI"
this.AutoSize = true
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
this.AcceptButton = btnAdd
this.StartPosition = FormStartPosition.CenterScreen
this.add_Shown(onShown)
this.ShowDialog()
} // FruitBasket constructor

} // FruitBasket dialog class

// Main entry point of program
new FruitBasket()


__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind

Other related posts: