list boxes in visual basic .net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am coding an application in .net using a list box. I have the listbox
property set to MultiSimple. I can click on several different selections (or
all) in the list box, but only the first one is the "active" one. What I
want to do is create an application that someone can order menu items from(I
am a beginning .net programmer). Here is my code that can allow one
selection:
'defines structure
Structure MenuItems
Public strItem As String
Public decPrice As Decimal
End Structure
'declare module level array
Dim mitmMenuItems(7) As MenuItems
Private Sub frmsmoke_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'fills mitmmenuitems array with menu items and prices from
'a sequential access file
'declare variables
Dim sreStreamReader As IO.StreamReader
Dim intNumber As Integer

Try 'turns on error trapping
'creates a streamreader object by opening text file for input
sreStreamReader = IO.File.OpenText("smoke.txt")

'process loop instructions until end of loop is encountered or _
'no more text to read
Do Until sreStreamReader.Peek = -1 _
OrElse intNumber >= mitmMenuItems.Length

'reads an items price and assign to array with info on
separate _
'lines in the file
mitmMenuItems(intNumber).strItem = sreStreamReader.ReadLine()
mitmMenuItems(intNumber).decPrice = _
Convert.ToDecimal(sreStreamReader.ReadLine())

'adds item to list box
Me.lstorder.Items.Add(mitmMenuItems(intNumber).strItem)

'updates array
intNumber = intNumber + 1

Loop

'closes file
sreStreamReader.Close()

Catch ex As Exception
MessageBox.Show(ex.Message, "Smoke Shack BBQ", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnexit.Click
'closes app
Me.Close()

End Sub

Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnclear.Click
'clears controls
Me.lblsub.Text = ""
Me.lbltax.Text = ""
Me.lbltotal.Text = ""
Me.lstorder.ClearSelected()

End Sub

Private Sub btncalc_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncalc.Click
'searches array for item selected in lstorder control
'declares variables
Dim strSearchFor As String
Dim intNumber As Integer
Dim decItemPrice, decTotal As Decimal

'initialize variable
strSearchFor = Convert.ToString(Me.lstorder.SelectedItem)

'add 1 to array subscript until items are located in array
Do Until mitmMenuItems(intNumber).strItem = strSearchFor
intNumber = intNumber + 1

Loop
How can I go about adding multiple selections and adding up all selections
to get a total price? Right now, I have changed the .SelectedItem to
..SelectedItems and it says that my array is out of bounds.

Any help would be appreciated,
Thanks,
Dave
 
Hi Dave,

I haven't studied your code much (VB isn't my strongest side), but there
is nothing magic about multiple ListBox selections.

string s = "";
foreach(string o in lisBox1.SelectedItems)
s += o + "\r\n";

This is C# but VB.Net code should be something similar.
Inside the foreach you could also switch the strings and sum up

foreach(string s in listBox1.SelectedItems)
{
switch(s)
{
case "Potatoes":
Price += PriceOfPotatoes;
break;
}
}

Or you could create your own menuitem and sum up the price

class MenuItem
{
private string Name;
public int Price;

public MenuItem(string n, int p)
{
Name = n;
Price = p;
}

public override string ToString()
{
return Name;
}
}

on button click and with the listbox filled with MenuItem

int price = 0;
foreach(MenuItem m in listBox1.SelectedItems)
{
price += m.Price;
}

The overridden ToString will ensure the Name is visible in the ListBox.
 
Back
Top