Results 1 to 8 of 8

Thread: Reverse the items in list box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    19

    Post Reverse the items in list box

    Hi,
    I am doing a project for production test unit. i need to reverse the order of items in the list box that is updating for each second ( to be simple the data must look like last in first out). here is the code that i am using for updating the items in the list box
    Code:
    lstData2.Items.Add(value & vbTab & txtStatus.Text & vbTab & Now)
                Me.lstData2.Text = Me.txtTemp.Text & vbNewLine & Me.lstData2.Text
                My.Computer.FileSystem.WriteAllText("C:\temp\test.txt", value & vbTab & Now & vbCrLf, True)
                If lstData2.Items.Count > 20 Then
                    Timer.Enabled = False
                    txtStatus.Text = "Dangerous Reading"
                    txtStatus.BackColor = Color.Red
                    lstData1.Text = "Limit Exceeded"
                    MsgBox("You can't add more Items")
                End If
    Can any one help me please

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    19

    Re: Reverse the items in list box

    I got it here is the answer, I found the code after posting this forum
    Code:
     Dim arr(lstData2.Items.Count - 1) As Object
    
            lstData2.Items.CopyTo(arr, 0)
            Array.Reverse(arr)
            lstData2.Items.Clear()
            lstData2.Items.AddRange(arr)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    19

    Re: Reverse the items in list box

    but I need to do same thing for two list boxes which I am not able to do form the above code. can any one help me please

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Reverse the items in list box

    Quote Originally Posted by sonu1992 View Post
    but I need to do same thing for two list boxes which I am not able to do form the above code. can any one help me please
    Why exactly can't that code be used on these other ListBoxes? Are we to guess? That code is going to work on any unbound ListBox so the only reason I can think of that it wouldn't work is that the ListBox is bound. You haven't mentioned that, nor have you mentioned the error that would have occurred using that code that would have confirmed that they were bound. If that code doesn't work then something that you don't expect happens instead. What? Are the ListBoxes bound? If so then you have to reverse the contents of the data source but how to do that depends on what it is.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    19

    Post Re: Reverse the items in list box

    Quote Originally Posted by jmcilhinney View Post
    Why exactly can't that code be used on these other ListBoxes? Are we to guess? That code is going to work on any unbound ListBox so the only reason I can think of that it wouldn't work is that the ListBox is bound. You haven't mentioned that, nor have you mentioned the error that would have occurred using that code that would have confirmed that they were bound. If that code doesn't work then something that you don't expect happens instead. What? Are the ListBoxes bound? If so then you have to reverse the contents of the data source but how to do that depends on what it is.
    Sorry I am not getting what are you asking me actually, I am very new to VB.Net that's why I am not able to post clearly about the problem that I am facing. But actually right now that code works. I am facing another problem now, I am not able to write the data from the text file to list box exactly in the same order they stored in the text file. I am getting the data into list box but not in an particular format like 0,1,2,3,.. it is displaying like 4,9,8,0,2,3,... can you help me on this issue please

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Reverse the items in list box

    Quote Originally Posted by sonu1992 View Post
    Sorry I am not getting what are you asking me actually, I am very new to VB.Net that's why I am not able to post clearly about the problem that I am facing. But actually right now that code works.
    Would you go to the doctor and expect a diagnosis without describing your symptoms? You don't need any programming experience to understand that if you want help with a problem then you explain what the problem is.
    Quote Originally Posted by sonu1992 View Post
    I am facing another problem now, I am not able to write the data from the text file to list box exactly in the same order they stored in the text file. I am getting the data into list box but not in an particular format like 0,1,2,3,.. it is displaying like 4,9,8,0,2,3,... can you help me on this issue please
    That's nothing to do with the topic of this thread so it belongs in a new thread of its own. One thread per topic and one topic per thread please.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    19

    Re: Reverse the items in list box

    ok thank you so much for your response. so I have to post a new thread for this topic.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Reverse the items in list box

    Quote Originally Posted by sonu1992 View Post
    so I have to post a new thread for this topic.
    Correct.

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