Results 1 to 4 of 4

Thread: [Resolved]Building string from listbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    [Resolved]Building string from listbox

    Does this code look correct to complete the task?

    Code:
    'store all medications information as string value
            While lstMeds.Items.Count > -1
    
                For i = 0 To lstMeds.Items.Count - 1
                    strActiveMeds = strActiveMeds & lstMeds.SelectedItem & ", "
                Next
            End While
    Thanks.
    Last edited by hipopony66; Feb 16th, 2009 at 05:46 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Building string from listbox

    First off, the while-loop will never reach an end because you arent removing items from the ListBox, thus not changing the lstMeds.Items.Count property.
    And even if you did, the Count property could never go lower than 0 so it'll always be greater than -1.

    You are also appending the same item to the string in each iteration in the for-loop; the selected item. You'd need to do something like this:
    Code:
                For i = 0 To lstMeds.Items.Count - 1
                    strActiveMeds = strActiveMeds & Cstr(lstMeds.Items(i))
                Next
    However its always a good idea to use a StringBuilder when building strings like this. Search for "StringBuilder" on MSDN and you'll find out how.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Building string from listbox

    agree with atheist.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Building string from listbox

    Thanks. I didn't know anything about the StringBuilder class!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width