• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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

0
hpops
Asked:
hpops
  • 2
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
The ComboBox has a few limitations. You've hit one.

The only way I know of doing that with the standard ComboBox is to store your data in a collection that enables to Insert an element at a given position, and use that collection as the DataSource for your ComboBox.

When you need to add an item, do not add it to the ComboBox. Insert it as the first element.

You've hit a second limitation of the ComboBox. If you add an element to the DataSource, it does not display. The ComboBox fills itself with the collection when you assign it to the DataSource, but does not follow additions afterward.

The solution is to set the DataSource to Nothing and then reassign your collection.
0
 
hpopsAuthor Commented:
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
0
 
Jacques Bourgeois (James Burger)Commented:
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.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now