vbMarkO Posted August 4, 2006 Posted August 4, 2006 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 Quote Visual Basic 2008 Express Edition!
MrPaul Posted August 4, 2006 Posted August 4, 2006 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: Quote Never trouble another for what you can do for yourself.
MrPaul Posted August 4, 2006 Posted August 4, 2006 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: Quote Never trouble another for what you can do for yourself.
vbMarkO Posted August 4, 2006 Author Posted August 4, 2006 Forgive my ignorance but how would I execute this code from a button click event my button is btnVisit vbMarkO Quote Visual Basic 2008 Express Edition!
Cags Posted August 4, 2006 Posted August 4, 2006 On a button click you just have' where DateTime.Now is the time you wish to add AddDate(DateTime.Now) Quote Anybody looking for a graduate programmer (Midlands, England)?
vbMarkO Posted August 4, 2006 Author Posted August 4, 2006 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 Quote Visual Basic 2008 Express Edition!
Cags Posted August 4, 2006 Posted August 4, 2006 Try using the Value property of the DateTimePicker, not the Text property. Quote Anybody looking for a graduate programmer (Midlands, England)?
MrPaul Posted August 5, 2006 Posted August 5, 2006 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. Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.