Solved

Visual Basic 2008 combobox history sort order

Posted on 2015-02-06
3
275 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#.NET and microsoft certification. 3 42
How useful is the free version of Selenium? 3 38
Where did System.Data.Objects go? 2 29
Modal Popup Extender control 1 22
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

831 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