Solved

Visual Basic 2008 combobox history sort order

Posted on 2015-02-06
3
262 Views
Last Modified: 2015-02-09
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
Comment
Question by:hpops
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40595231
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
 

Author Closing Comment

by:hpops
ID: 40599029
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
 
LVL 40
ID: 40599097
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now