Link to home
Start Free TrialLog in
Avatar of hpops
hpops

asked on

Visual Basic 2008 combobox history sort order

I have some code working that saves the history of entered data into the Combobox field. It is placing the last entered data at the end of the list. I'd rather have it placed at the beginning of the list instead.

 I looked at ComboboxSortedProperty on MSDN but they mention that turning Sorted=True on sorts the data alphabetically ascending order. I simply need to reverse the order of data presented when Sorted = False

Does anyone know if it's possible to do this?

Public Class Form1

    'Read Settings

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each item As String In My.Settings.ComboBoxItems
            ComboBox1.Items.Add(item)
        Next
    End Sub
    'Write Settings

    Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            If ComboBox1.Text.Trim <> "" And Not Me.ComboBox1.Items.Contains(ComboBox1.Text) Then
                Me.ComboBox1.Items.Add(ComboBox1.Text)
                My.Settings.ComboBoxItems.Add(ComboBox1.Text)
            End If
        End If
    End Sub
    'Save Settings

    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        My.Settings.Save()
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hpops
hpops

ASKER

James,

Thanks for the clarification and quick response. I was hoping that there might be a way to reverse order but we'll get by.

Thanks again,
Dave
Arrays and some collections do have a reverse method. But they reverse the whole thing, so as I understand what you want to do, it would do not good because you are inserting the entries one by one. The first reversal would to the job, but the second one would bring back the list in the order that you do not want to have.

Also, reversing probably forces the system to go through the whole list to reverse the order, while inserting deals with only one element. So I think that inserting would be faster.

And while testing to make sure that what I say here is OK (memory being the thing that forgets the most in the world), I saw that there is an Insert method on ComboBox.Items. I was not aware of that because I always start my ComboBoxes with a collection of data that comes from somewhere. So you might simply use that Insert method to add something at the top of the list.