Solved

Visual Basic 2008 combobox history sort order

Posted on 2015-02-06
3
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.

724 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