Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Visual Basic 2008 Express Edition!
Posted

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

 

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:

 

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:

Never trouble another for what you can do for yourself.
Posted

Playing for the other team...

 

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

 

        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:

Never trouble another for what you can do for yourself.
Posted

Forgive my ignorance but how would I execute this code from a button click event

 

my button is btnVisit

 

vbMarkO

Visual Basic 2008 Express Edition!
Posted
On a button click you just have
' where DateTime.Now is the time you wish to add
AddDate(DateTime.Now)

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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

Visual Basic 2008 Express Edition!
Posted
Try using the Value property of the DateTimePicker, not the Text property.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.
Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...