listbox sorting?

vbMarkO

Centurion
Joined
Aug 19, 2005
Messages
157
I am loading Dates into a listbox....

At the moment I have dtPick.value which is 8/4/2006 12:39 AM

OK but what I want is Friday, August 04, 2006

The problem is I think is this is not as easy to keep in a sorted order

I am sure this has been asked before but cant find anything specific but will keep looking.

Anyway, Is there a way somehow to do a comparison beofer the date is added to the listbox so that it places it in the right order or is there a easier way to do it that I am just not aware of?

vbMarkO
 
To control formatting, I would wrap the date value in a custom object:

C#:
private class ListboxDateItem
{
    //Fields
    public readonly DateTime Date;

    //Constructors
    public ListboxDateItem(DateTime date)
    {
        Date = date;
    }

    //Methods
    public override string ToString()
    {
        return Date.ToString("dddd, MMMM MM, yyyy");
    }
}

Then when adding a date, I would suggest iterating through the collection until a later date is encountered, and insert the new date before that item:

C#:
private void AddDate(DateTime newdate)
{
    int index = 0;

    foreach (ListboxDateItem lbd in listBox1.Items) {
        if (lbd.Date > newdate) break;
        index++;
    }

    listBox1.Items.Insert(index, new ListboxDateItem(newdate));
}

Good luck :cool:
 
Playing for the other team...

Apologies, I forgot you are using Visual Basic. Here is a translation.

Visual Basic:
        Private Class ListboxDateItem
            'Fields
            Public ReadOnly [Date] As DateTime

            'Constructors
            Public Sub New(ByVal thedate As DateTime)
                [Date] = thedate
            End Sub

            'Methods
            Public Overrides Function ToString() As String
                Return [Date].ToString("dddd, MMMM MM, yyyy")
            End Function
        End Class

        Private Sub AddDate(ByVal newdate As DateTime)
            Dim index As Integer = 0

            For Each ldi As ListboxDateItem In Listbox1.Items
                If (ldi.Date > newdate) Then Exit For
                index = index + 1
            Next

            Listbox1.Items.Insert(index, New ListboxDateItem(newdate))
        End Sub

Good luck :cool:
 
Forgive my ignorance but how would I execute this code from a button click event

my button is btnVisit

vbMarkO
 
OK tried this and this is what I got

I put AddDate(DateTIme.Now) int he btnVisit_Click event

result was this; Unable to cast object of type 'System.String' to type 'ListboxDateItem'.

The dates being added to the listbox are from a DatePicker

User selects Date in dtPick <--- DatePicker

the btnVisit click event then adds the date that is in the dtPick.Text field

The dates added to lstDates are in this format Friday, August 04, 2006

What I am needing is to make sure the dates stay in a sorted order...

vbMarkO
 
This method will only work if every item in the listbox is an instance of ListboxDateItem. Judging by the exception you're seeing, I would guess that there are items in the listbox which have been inserted as strings.
 
Back
Top