9 Replies - 2540 Views - Last Post: 11 October 2012 - 11:12 AM Rate Topic: -----

#1 jskidd3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-September 12

Reverse Combo Box Items

Posted 11 October 2012 - 02:13 AM

Hi!

I have something relatively simple to solve, but to be totally honest I have never done this kind of thing before and can't find a way to do it.

All I need to do is reverse the order in which a combobox is displaying items. To be clear:

Before:
Duck
Horse
Cow

After:
Cow
Horse
Duck

I attempted using .reverse and couldn't seem to do it. If anyone could help I'd be very grateful

Regards,
Joel

Is This A Good Question/Topic? 0
  • +

Replies To: Reverse Combo Box Items

#2 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: Reverse Combo Box Items

Posted 11 October 2012 - 03:24 AM

Dont think Combobox has a reverse function, you can create a new arraylist, copy out the elements to the arraylist and then use the .reverse function on that, then clear the combobox and reload from the arraylist. Just an idea.
Was This Post Helpful? 0
  • +
  • -

#3 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Reverse Combo Box Items

Posted 11 October 2012 - 03:32 AM

agreed with peterH

but there is another way ,you can do it.. by simply
Specify the location in the AddItem statement:


Private Sub Command1_Click()
Combo1.AddItem "Duck", 3
End Sub

Private Sub Form_Load()
' Sample Data
For I = 1 To 10 ' you can specify your own value (1 to 3) 3 items..
    Combo1.AddItem Str$(I)
Next
End Sub



This post has been edited by kai_itz me: 11 October 2012 - 03:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 jskidd3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-September 12

Re: Reverse Combo Box Items

Posted 11 October 2012 - 04:30 AM

Ok thanks guys, annoyingly because there are over 100 results it would be annoying to do that, kai. As for Peter's suggestion, I'd have no idea how to approach creating the array, I'll have to research it a bit more first.

Thanks guys. =)
Was This Post Helpful? 0
  • +
  • -

#5 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: Reverse Combo Box Items

Posted 11 October 2012 - 05:27 AM

Its really straighforward,something like should work, assuming the sorted property of the combobox is not set to true

Dim templist As New ArrayList() ' create a new arraylist
Dim a As Integer
For a = 0 To cbo.Items.count - 1
    templist.add(cbo.items.item(a)) ' move the combobox items into the arraylist
next
cbo.items.clear() ' call the clear method to remove all items from the combobox
templist.reverse() call the reverse method to reverse the order of the items in the arraylist
For a = 0 To templist.Count - 1 
    cbo.Items.Add(templist(a)) ' move the arraylist items back to the combobox
next 


Was This Post Helpful? 0
  • +
  • -

#6 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: Reverse Combo Box Items

Posted 11 October 2012 - 05:30 AM

i read about this once, and remember them mentioning using a hidden listbox in conjunction with the combobox. sort em in the listbox then display em in the combobox. Dunno, trying to find the place i read it.
Was This Post Helpful? 0
  • +
  • -

#7 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 09-October 12

Re: Reverse Combo Box Items

Posted 11 October 2012 - 05:53 AM

You better use a Sub in order to have the ability to reverse items of multiple ComboBoxes.

    ''' <summary>
    ''' Reverse ComboBox Items' Order.
    ''' </summary>
    ''' <param name="ComboBox">Required. ComboBox to reverse items from.</param>
    Public Shared Sub ReverseComboBoxItems(ByVal ComboBox As ComboBox)
        ' Create a new ArrayList used to reverse items.
        Dim List As New ArrayList
        ' Add all items from the ComboBox to the ArrayList.
        List.AddRange(ComboBox.Items)
        ' Reverse items.
        List.Reverse()
        ' Remove all items from ComboBox.
        ComboBox.Items.Clear()
        For Index As Integer = 0 To List.Count - 1
            ' Add reversed items again to the ComboBox
            ComboBox.Items.Add(List.Item(Index))
        Next
        ' Clear List for later use.
        List.Clear()
    End Sub


Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Reverse Combo Box Items

Posted 11 October 2012 - 07:45 AM

A simple-to-solve problem should have a simple solution. Here's one.

    Private Sub testButton_Click(sender As System.Object, e As System.EventArgs) Handles LoginButton.Click
        ComboBox1.Items.Add("Horse")
        ComboBox1.Items.Add("Cow")
        ComboBox1.Items.Add("Unicorn")
        Dim temp As String
        Dim itemCount As Integer = ComboBox1.Items.Count
        For i As Integer = itemCount - 2 To 0 Step -1
            temp = CStr(ComboBox1.Items.Add(ComboBox1.Items.Item(i)))
            ComboBox1.Items.RemoveAt(i)
        Next
    End Sub


This post has been edited by lar3ry: 11 October 2012 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Reverse Combo Box Items

Posted 11 October 2012 - 09:33 AM

View Postlar3ry, on 11 October 2012 - 07:45 AM, said:

A simple-to-solve problem should have a simple solution. Here's one.


Or, even easier <shaking head>. Just add a module to your project and use this code.
It will allow you to call it with (for example:
ComboBox1.Reverse()

Alternatively, you could just use the subroutine part and call it with:
Reverse(ComboBox1)

Imports System.Runtime.CompilerServices

Module Extensions

    <Extension()>
    Public Sub Reverse(ByVal cbox As ComboBox)
        Dim itemCount As Integer = cbox.Items.Count
        For i As Integer = itemCount - 2 To 0 Step -1
            cbox.Items.Add(cbox.Items.Item(i))
            cbox.Items.RemoveAt(i)
        Next
    End Sub

End Module



Was This Post Helpful? 0
  • +
  • -

#10 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Reverse Combo Box Items

Posted 11 October 2012 - 11:12 AM

OK, just because it's interesting, you can test this little snippet. All you need is new Windows Forms project with this code:

Public Class Form1
    Dim myList As New List(Of String) From {"Duck", "Horse", "Cow"}
    Dim WithEvents binding As New BindingSource With {.DataSource = myList}
    Dim WithEvents myCmbBox As New ComboBox
    Dim WithEvents btnCmbReverse As New Button With {.Text = "Reverse ComboBox Items!", .Height = 50}

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Controls.AddRange({myCmbBox, btnCmbReverse})
        myCmbBox.Location = New Point(5, 5)
        myCmbBox.DataSource = binding
        btnCmbReverse.Location = New Point(myCmbBox.Right + 5, 5)
        With Me
            .Height = 100
            .Width = btnCmbReverse.Right + 20
        End With
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnCmbReverse.Click
        myList.Reverse()
        binding.ResetBindings(True)
    End Sub
End Class



Have fun with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1