Re: Minimalist fruit basket program in VB2005

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sat, 10 Nov 2007 20:51:50 -0500 (EST)

Yes, I've turned into a fruit cake!

Below is an even shorter VB version that uses a TableLayoutPanel instead
of specifying control coordinates.  I found that the compiler could not
infer types with the AddRange method, so an array of Control objects had
to be stated explicitly.  Also note that a VB program requires at least
one class and a Main method with in it, unlike Boo (or , I think, JScript
.NET and Nemerle).  VB also needs at least one event handler method
whereas C#, Boo, and Nemerle can define an in-line event handler (a method
without a name, also called an anonymous delegate).

Jamal

' content of vb_fruit.vb
' Fruit Basket program in Visual Basic .NET
' Public domain by Jamal Mazrui

' Import namespace
Imports System.Windows.Forms

Module FruitBasket
' Initialize controls
Dim tlp = new TableLayoutPanel()
Dim lblFruit = new Label()
Dim txtFruit = new TextBox()
Dim WithEvents btnAdd as Button = new Button()
Dim lblBasket = new Label()
Dim lstBasket = new ListBox()
Dim WithEvents btnDelete as Button = new Button()
Dim dlg = new Form()

' Define entry point of program
Sub Main()
' Specify control properties
With tlp
.ColumnCount = 3 : .RowCount = 2 : .Parent = dlg
.Controls.AddRange( new Control() {lblFruit, txtFruit, btnAdd, lblBasket,
lstBasket, btnDelete})
End With

lblFruit.Text = "&Fruit:" : lblBasket.Text = "&Basket:"
btnAdd.Text = "&Add" : btnDelete.Text = "&Delete"

With dlg
.Text = "Fruit Basket" : .AcceptButton = btnAdd : .StartPosition =
FormStartPosition.CenterScreen : .AutoSize = True : .AutoSizeMode =
AutoSizeMode.GrowAndShrink
.ShowDialog()
End With
End Sub

' Define event handler
Sub Button_Click(sender as Object, e as EventArgs) Handles btnAdd.Click,
btnDelete.Click
Select Case sender.Text
case "&Add"
Dim sFruit = txtFruit.Text
if sFruit = "" Then
MessageBox.Show("No fruit to add!", "Alert")
Return
End If

lstBasket.Items.Add(sFruit)
txtFruit.Text = ""
lstBasket.SelectedIndex = lstBasket.Items.Count - 1

Case "&Delete"
Dim iFruit = lstBasket.SelectedIndex
if iFruit = (-1) Then
MessageBox.Show("No fruit to delete.", "Alert")
Return
End If

lstBasket.Items.RemoveAt(iFruit)
if iFruit > (lstBasket.Items.Count - 1) Then iFruit =
(lstBasket.Items.Count - 1)
lstBasket.SelectedIndex = iFruit
End Select
End Sub
End Module


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

Other related posts: