Re: Optimized fruit basket program in Boo

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 11 Nov 2007 09:32:08 -0500 (EST)

Fruit cake here again .... <grin>

Iterating back to Boo, I tried to simplify the code further using the
TableLayoutPanel idea from the last VB program.  The result is my shortest
and cleanest fruit basket program to date -- by a significant margin.

Note that although no types are explicitly declared, the Boo compiler
inferred them by introspection on the .NET Framework Class Library.
 Also note that although Python-like indentation is required for
subordinate blocks of code, the program only needed this by one level for
the body of each event handler.  Since lines may wrap in this email, I
have also added the source code to the archive at
http://www.EmpowermentZone.com/boo_fruit.zip

Jamal

/*
content of simpler.boo
Fruit Basket program in Boo
Public domain by Jamal Mazrui
*/

// Import namespaces
import System
import System.Windows.Forms

// Create controls
tlp = TableLayoutPanel(ColumnCount : 3, RowCount : 2)
lblFruit = Label(Text : "&Fruit:")
txtFruit = TextBox()
btnAdd = Button(Text : "&Add")
lblBasket = Label(Text : "&Basket:")
lstBasket = ListBox()
btnDelete = Button(Text : "&Delete")
tlp.Controls.AddRange((lblFruit, txtFruit, btnAdd, lblBasket, lstBasket,
btnDelete))

// Define Add event handler
btnAdd.Click += def (o, e):
  sFruit = txtFruit.Text
  if sFruit == "": return MessageBox.Show("No fruit to add!", "Alert")
  lstBasket.Items.Add(sFruit)
  txtFruit.Text = ""
  lstBasket.SelectedIndex = (lstBasket.Items.Count - 1)

// Define Delete event handler
btnDelete.Click += def (o, e):
  iFruit = lstBasket.SelectedIndex
  if iFruit == -1 : return MessageBox.Show("No fruit to delete.", "Alert")
  lstBasket.Items.RemoveAt(iFruit)
  if iFruit > (lstBasket.Items.Count - 1): iFruit = (lstBasket.Items.Count
- 1)
  lstBasket.SelectedIndex = iFruit

// Finalize dialog
dlg = Form(Text : "Fruit Basket", AcceptButton : btnAdd, StartPosition :
FormStartPosition.CenterScreen, AutoSize : true, AutoSizeMode :
AutoSizeMode.GrowAndShrink)
dlg.Controls.Add(tlp)
dlg.ShowDialog()

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

Other related posts: