Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2005

Tip: Looking for answers? Try searching our database.

list boxes in visual basic .net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dave - 27 Feb 2005 22:19 GMT
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
Morten Wennevik - 28 Feb 2005 11:21 GMT
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.

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.