Minimalist fruit basket program in C# 3.0

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 21 Nov 2007 08:42:43 -0500 (EST)

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

This fruit basket program is written in C# 3.0:  a language update
officially released by Microsoft along with the .NET Framework 3.5 and
Visual Studio 2008.  Visual C# 2008 Express is a free download that
includes the new .NET Framework and SDK.

The archive includes a batch file, compile.bat, which invokes the
command-line compiler to create an executable, cs3_fruit.exe, which is
about 6K in size.  No other files are needed to run the program -- as long
as .NET 3.5 is installed.

Language enhancements to C# 3.0 include functional and dynamic programming
constructs, in-line data queries, inferred typing of local variables, and
property initialization when calling an object constructor.  A technical
overview is at
http://msdn2.microsoft.com/en-us/library/bb308966.aspx

This program uses new language features to be minimalist in the code
required to meet the fruit basket specification at
http://FruitBasketDemo.AlacornComputer.com

No variable types are explicitly declared, each control is specified in a
single statement, and lambda expressions are used for event handlers.  The
included source code is also pasted below.

Jamal

/*
content of cs3_fruit.cs
Fruit Basket program in C# 3.0
Public domain by Jamal Mazrui
*/

// Import namespaces
using System;
using System.Windows.Forms;

// Define class
class FruitBasket {

// Define entry point of program
static void Main() {
// Create controls;
var tlp = new TableLayoutPanel {ColumnCount = 3, RowCount = 2};
var lblFruit = new Label {Text = "&Fruit:", Parent = tlp};
var txtFruit = new TextBox {Parent = tlp};
var btnAdd = new Button {Text = "&Add", Parent = tlp};
var lblBasket = new Label {Text = "&Basket:", Parent = tlp};
var lstBasket = new ListBox {Parent = tlp};
var btnDelete = new Button {Text = "&Delete", Parent = tlp};

// Define Add event handler;
btnAdd.Click += (o, e) => {
var sFruit = txtFruit.Text.Trim();
if (sFruit == "") MessageBox.Show("No fruit to add!", "Alert");
else {
lstBasket.Items.Add(sFruit);
txtFruit.Clear();
lstBasket.SelectedIndex = lstBasket.Items.Count - 1;
}
};

// Define Delete event handler;
btnDelete.Click += (o,e) => {
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;
}
};

// Finalize dialog;
var dlg = new Form {Text = "Fruit Basket", AcceptButton = btnAdd,
StartPosition = FormStartPosition.CenterScreen, AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink};
dlg.Controls.Add(tlp);
// Define closing event handler
dlg.Closing += (o, e) => e.Cancel = (MessageBox.Show("Close program?",
"Confirm", MessageBoxButtons.YesNo) == DialogResult.No);
dlg.ShowDialog();
} // Main method
} // FruitBasket class

// End of cs3_fruit.cs

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

Other related posts: