listbox and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saurabhkunal
    New Member
    • Mar 2008
    • 4

    listbox and arrays

    how to move the contents of listbox to arrays and get highest two entries...
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Listboxs already store there items in an array (well, Collection, but they work the same for most things). You can just access index 0 and 1 (or 1 and 2 if in VBNET?) of the items.

    Comment

    • todashah
      New Member
      • Feb 2008
      • 26

      #3
      As far as C# is concern write down following code to copy content
      of listbox to array....

      int k = listBox1.Items. Count;
      int[] i = new int[10];
      int a = 0;
      for (int j = 0; j < k; j++)
      {
      i[a++] = int.Parse(listB ox1.Items[j].ToString());
      }

      Here i have assume that listbox contain integer value. Once data store
      in array now i have left remaining work for u to find two highest value..

      Bye....Enjoy Coding

      Comment

      • saurabhkunal
        New Member
        • Mar 2008
        • 4

        #4
        Originally posted by todashah
        As far as C# is concern write down following code to copy content
        of listbox to array....

        int k = listBox1.Items. Count;
        int[] i = new int[10];
        int a = 0;
        for (int j = 0; j < k; j++)
        {
        i[a++] = int.Parse(listB ox1.Items[j].ToString());
        }

        Here i have assume that listbox contain integer value. Once data store
        in array now i have left remaining work for u to find two highest value..

        Bye....Enjoy Coding
        can u please tell me how to write the same code in vb.net....

        Comment

        • saurabhkunal
          New Member
          • Mar 2008
          • 4

          #5
          Originally posted by todashah
          As far as C# is concern write down following code to copy content
          of listbox to array....

          int k = listBox1.Items. Count;
          int[] i = new int[10];
          int a = 0;
          for (int j = 0; j < k; j++)
          {
          i[a++] = int.Parse(listB ox1.Items[j].ToString());
          }

          Here i have assume that listbox contain integer value. Once data store
          in array now i have left remaining work for u to find two highest value..

          Bye....Enjoy Coding
          can u please tell me how to write the code in vb.net.actually i need the above code in vb.net.....

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            It's pretty straight forward.

            This should be code for using the "top" two elements.
            [code=vbnet]
            Dim mystr1 as String("")
            Dim mystr2 as String("")

            'I think Array indexes in VB start with 1, if they start with 0 like they should
            'Then just change the 1 and 2 to 0 and 1
            mystr1=listBox1 .Items(1).ToStr ing()
            mystr1=listBox1 .Items(2).ToStr ing()
            [/code]

            Comment

            • saurabhkunal
              New Member
              • Mar 2008
              • 4

              #7
              Originally posted by Plater
              It's pretty straight forward.

              This should be code for using the "top" two elements.
              [code=vbnet]
              Dim mystr1 as String("")
              Dim mystr2 as String("")

              'I think Array indexes in VB start with 1, if they start with 0 like they should
              'Then just change the 1 and 2 to 0 and 1
              mystr1=listBox1 .Items(1).ToStr ing()
              mystr1=listBox1 .Items(2).ToStr ing()
              [/code]
              i have retrieved 3 marks of a student and i have to select best two
              out of them.i used the sort property of listbox so that marks comes
              in sorted order and i can take the best two marks but its not working.
              how can i take best two marks from the listbox.another way which i thought
              was to transfer the contents of listbox to array sort it and take the values
              at indices 1 and 2 which will be highest and second highest
              but i was not able to write the code for that.how can i
              do this or can u tell me any other method to do this....

              Comment

              Working...