7 Replies - 423 Views - Last Post: 25 January 2014 - 10:49 AM Rate Topic: -----

#1 Leftic  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 18-July 13

Sorting And Ordering with a listbox?

Posted 23 January 2014 - 12:27 PM

Alright, I looked around Google and couldnt find a simple solution to what I want to do.

I have several radio buttons:
"Sort A - Z"
"Sort Z - A"
"Sort 0 - 9"
"Sort 9 - 0"
... You get the idea

So, as I was coding it, I realized, that there is no simple command for listboxes like "listbox1.items.order(0,9)" to say starting index being 0, and ending index being 9, so every item within that range will then be ordered from 0 - 9

So, how might I set sorting/ordering from 0 - 9, 9 - 0, a - z, z - a, A - Z, and Z - A?

Thanks for any help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting And Ordering with a listbox?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9091
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Sorting And Ordering with a listbox?

Posted 23 January 2014 - 12:31 PM

I would figure you need to create a custom listbox (that inherits listbox) and override the sort mechanism.

MSDN seems to agree with that idea.

Quote

Notes to Inheritors

You can override this method in your derived class to provide your own sorting routine. Access the results of the overridden Sort method by setting the Sorted property to true. When adding items to a ListBox, it is more efficient to sort the items first and then add new items.

http://msdn.microsof...=vs.110%29.aspx
Was This Post Helpful? 0
  • +
  • -

#3 Minimalist  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 104
  • Joined: 13-November 13

Re: Sorting And Ordering with a listbox?

Posted 23 January 2014 - 02:15 PM

The listbox has a sorted property. So in your radiobutton click method you can set the listbox to ...sorted=true whic will sort the list box. To rverse the sort you put in the 2nd radiobutton ... you create an array, copy the sorted list into the array,set your lisbox....sorted = false , clear the listbox and copy the items of the array from the highest index to = into the listbox. This takes care of your words.
Was This Post Helpful? 0
  • +
  • -

#4 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,204
  • Joined: 11-May 12

Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 06:46 AM

Another option is to create a List(Of String) and populate that with your listbox items, then sort the List(Of String) using .sort, then clear and re-add the items back into the listbox.
Was This Post Helpful? 0
  • +
  • -

#5 Leftic  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 18-July 13

Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 03:15 PM

None of that seemed to help. Thank you for the suggestions though.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9091
  • View blog
  • Posts: 34,142
  • Joined: 12-June 08

Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 03:22 PM

Did you even try overloading the control?
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Sorting And Ordering with a listbox?

Posted 24 January 2014 - 03:29 PM

You could also show us some results from your attempts and show why they didn't work for you. Perhaps, it's something to the effect that you list sorted 0-9 like
1
11
2
21
3
4
5
6
instead of
1
2
3
4
5
6
11
21

which is due to the fact that the listbox is seeing the numbers as a string representation of a number which would cause this kind of sort. So instead of telling us that it didn't work, perhaps you could give a little more insight as to why your attempt is not producing desired results and give someone something more to work with.
Was This Post Helpful? 0
  • +
  • -

#8 Leftic  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 18-July 13

Re: Sorting And Ordering with a listbox?

Posted 25 January 2014 - 10:49 AM

Theres no actual physical errors, only logical ones.
For example: When I tried to sort it myself, it either did not move anything, or it would just copy the last or 2nd last item to the first item in the list.

And overloading the control wouldnt work because the 6 types of options I want to do requires more then just a simple sort, then sort - 1 (for example for A - Z then Z - A)... At least that I could find while testing it, so I attempted to make my own, which is where it either does nothing or copies the 2nd last item and pastes it to the top.

Here is my code:

itterate = 0
        Try
            If RadioButton1.Checked = True Then
                For Each title In ListBox1.Items
                    itterate += 1
                    If ListBox1.Items(itterate).ToString.StartsWith("0") Then
                        'ListBox1.Items.RemoveAt(itterate)
                        ListBox1.Items.Insert(1, ListBox1.Items(itterate))
                    End If
                    If ListBox1.Items(itterate).ToString.StartsWith("1") Then
                        'ListBox1.Items.RemoveAt(itterate)
                        ListBox1.Items.Insert(2, ListBox1.Items(itterate))
                    End If
                    If ListBox1.Items(itterate).ToString.StartsWith("2") Then
                        'ListBox1.Items.RemoveAt(itterate)
                        ListBox1.Items.Insert(3, ListBox1.Items(itterate))
                    End If
                Next
            Else

            End If
        Catch
            Me.Show()
        End Try



This is all in the RadioButton1's Check Changed event.
"itterate" was already declared earlier at the top of my code, which is why you do not see a "Dim itterate = 0" there.

I started at 0 instead of -1 because I do not want the very first item of the listbox to ever be affected, as other parts of my program rely on it being and staying as the 0 index.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1