listbox in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khaled1988
    New Member
    • Aug 2008
    • 6

    listbox in c#

    i want the right code for this one

    i need to display listbox items one by one with coma (,) but the last item i have

    to keep it without coma ....

    for (i = 0; i <= listBox1.Items. Count; i++)
    {

    if (i == listBox1.Items. Count - 1)
    {
    url += listBox1.Items[i].ToString() ;
    }
    else
    url += listBox1.Items[i].ToString() + ",";
    }

    thanks
  • arunbojan
    New Member
    • Jul 2008
    • 30

    #2
    Hi,

    Can you be more specific or tell me what exactly is your required output and where you have to display...

    Comment

    • deathprincess
      New Member
      • Jul 2008
      • 12

      #3
      I assume you have declared all your variables. I think you should omit the "=" sign on (i <= listBox1.Items. Count). It will cause an argument null exception because the index of the listbox will be greater than the number of items on the collection. Hope this helps.

      Good Luck ^_^

      Comment

      • khaled1988
        New Member
        • Aug 2008
        • 6

        #4
        i need to store it in string verbal

        string url;
        foreach (object x in listBox1.Items)
        {
        if (==here is the problem== )
        url = url + x.ToString();
        else
        url = url + x.ToString() + ",";
        }
        i have to get the last item to store it in to url string with out coma ","

        this is the only problem

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Just add everything including the last comma then remove the last character in the string which will be the comma.
          Also realize that concatenating strings is best done using StringBuilder rather than the + operator on strings.

          Comment

          • khaled1988
            New Member
            • Aug 2008
            • 6

            #6
            please can you show me small sample

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by khaled1988
              please can you show me small sample

              Comment

              • khaled1988
                New Member
                • Aug 2008
                • 6

                #8
                thanks so much i got it

                Comment

                Working...