Important alert: (current site time 7/16/2013 5:35:09 AM EDT)
 

VB icon

Add Dates to treeview

Email
Submitted on: 8/13/2002 6:33:38 PM
By: Brett Perry 
Level: Intermediate
User Rating: Unrated
Compatibility: Delphi 5
Views: 11108
(About the author)
 
     Add months, years and days into a logical Treeview which is a parameter.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
!**************************************
! Name: Add Dates to treeview
! Description:Add months, years and days into a logical Treeview which is a parameter.
! By: Brett Perry
!
! Inputs:TTreeview, Format and NumberOfYears
!
! Returns:Dates upto the year 9999
!
! Assumes:The code adds dates to Treeview
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=770&lngWId=7!for details.!**************************************

procedure TfrmMainHair.FillTreeViewWithDates(Tree : TTreeview; Format : String; NumberOfYears : Integer);
var
	h, i, j, z : Integer;
 sDate : String;
 Year, Month, Day : Word;
 YearNode, MonthNode, DayNode : TTreeNode;
Const
	MonthsInYear : Array [0..11] of String = ('January', 'February', 'March', 'April', 'May',
 																				 'June', 'July', 'August', 'September',
'October', 'November', 'December');
	DaysInMonth	 : Array [0..11] of Integer = (31,28,31,30,31,30,31,31,30,31,30,31);
begin
	YearNode := Nil;
 DecodeDate(Date, Year, Month, Day);
 {set default integers}
 z := 0;
 Tree.Items.BeginUpdate;
 {years will be a variable that the user can configure, make sure user does not select years > 9999}
 for h := 0 to NumberOfYears - 1 do
 begin
 	if NumberOfYears > 1 then
begin
 		YearNode := Tree.Items.Add(Nil,IntToStr(Year));// Child(Tree.TopItem, IntToStr(Year));
	YearNode.ImageIndex := 0;
	YearNode.SelectedIndex := 0;
end;
{loop for how ever many months}
for i := 0 to high(MonthsInYear) - (high(MonthsInYear) - 2) do
begin
 {add a new month each pass}
	 	if NumberOfYears > 1 then
	 MonthNode := Tree.Items.AddChild(YearNode, MonthsInYear[Month - 1] + ' ' + IntToStr(Year))
			else MonthNode := Tree.Items.AddChild(Nil, MonthsInYear[Month - 1] + ' ' + IntToStr(Year));
 MonthNode.ImageIndex := 0;
 MonthNode.SelectedIndex := 0;
 {need to check for a leap year and increase the days in February}
 if (IsLeapYear(Year) and (Month = 2)) then
DaysInMonth[1] := 29
 {make sure to change bak to 28 days after a leap year}
 else DaysInMonth[1] := 28;
 {now loop for each day of the month}
 for j := 0 to (DaysInMonth[Month - 1] - Day) do
 begin
DateTimeToString(sDate,Format,IncrementDate(Date,z));
DayNode := Tree.Items.AddChild(MonthNode, sDate);
DayNode.ImageIndex := 3;
DayNode.SelectedIndex := 3;
{increase each, therefore getting the correct index}
inc(z);
 end;
 {check if we are at the end of the coming year}
 DecodeDate(IncrementDate(Date,z - 1), Year, Month, Day);
 {if we are increase the year}
 if ((Month = 12) and (Day = 31)) then
inc(Year);
 {now increase the month}
 inc(Month);
 {if we are in December change the month back to January}
 if month = 13 then month := 1;
 {set day as the first of the month}
 Day := 1;
			EncodeDate(Year, Month, Day);
end;
	end;
	{expand first level of tree and only its child}
 Tree.Items.Item[0].Expand(False);
 Tree.Items.EndUpdate;
 Tree.Items.Item[1].Selected := True;
end;


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.