Solved

trying to Put  Calendar days no  in stringGrid

Posted on 2010-09-19
11
1,433 Views
Last Modified: 2012-06-27
HELLO  ALL

Iam trying to create a small calendar

i did it on using image array  it work so good

But,,,  I want it to be in StringGrid  i Could not do it

her is the code :


//Procedure TForm1.CalculateDates;
//Var
//i,WeekDay: Byte;
//iCol, iRow: integer;
//Begin
//Screen.Cursor:=crHourGlass;
//Shape1.Visible:=False;
//WeekDay:=1;
//DateSeparator:='.';
//GD:=1;
//For i:=1 to 37 do begin
//with ImgArray do begin
//Picture.Bitmap.Canvas.Brush.Style:=bsClear;
//ADate:=StrToDate('1.'+'9'+'.'+'2010');
//If (GD=1) then begin
//If (DayOfWeek(ADate)=WeekDay) Then begin
//Picture.Bitmap.Canvas.Font.Color:=clRed;
//Picture.Bitmap.Canvas.Font.Size:=10;
//Picture.Bitmap.Canvas.TextOut(15,1,IntToStr(GD));
//Tag:=GD;
//GD:=GD+1;
//End else begin
//Cursor:=crDefault;
//Tag:=0;
//WeekDay:=WeekDay+1;
//Hint:='';
//End;
//End else if (GD<=NrOfDays) and (GD>1) then begin
//Picture.Bitmap.Canvas.Font.Color:=clBlack;
//Picture.Bitmap.Canvas.Font.Size:=10;
//Picture.Bitmap.Canvas.TextOut(15,1,IntToStr(GD));

//for iCol := 0 to 37 do// this I try but will not work
//for iRow := 1 to 37 do// this I try but will not work
//StringGrid1.Cells[iCol,iRow] := IntToStr(GD); // this I try but will not work

//Tag:=GD;
//GD:=GD+1;
//End else begin
//Tag:=0;
//WeekDay:=WeekDay+1;
//Hint:='';
//End; // if
//End;  // with
//End;  //For
//FindFirstDayOfMonth;
//Screen.Cursor:=crDefault;
//End;

iam trying to get the day no(GD) in stringGrid

Please Help ... Thanks all


0
Comment
Question by:zaferms
  • 5
  • 4
  • 2
11 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 33712643
add DateUtils in your uses clause and  call this function for any date

procedure TForm1.DrawDatesInGrid(ADateTime: TDateTime);
const
  DayNames: array[1..7] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat');
var
  i, noofdays, d, rowno, colno, rowcount: integer;
  firstday: TDateTime;
begin
  for I := 1 to 7 do
    StringGrid1.Cells[i, 0] := DayNames[i];

  colno := DayOfWeek(StartOfTheMonth(ADateTime));
  rowcount := WeekOfTheMonth(EndOfTheMonth(ADateTime)) + 1;

  noofdays := DaysInMonth(ADateTime);
  d := 0;
  rowno := 1;

  while rowno <= rowcount do
  begin
    while colno < 8 do
    begin
      Inc(d);
      StringGrid1.Cells[colno, rowno] := IntToStr(d);
      inc(colno);
      if d = noofdays then
        Exit;
    end;
    colno := 1;
    inc(rowno);
  end;
end;

//for current month call
DrawDatesInGrid(Now)
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33712652
You need to set the grids row and column count

procedure TForm1.DrawDatesInGrid(ADateTime: TDateTime);
const
  DayNames: array[1..7] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat');
var
  i, noofdays, d, rowno, colno, rowcount: integer;
  firstday: TDateTime;
begin
  colno := DayOfWeek(StartOfTheMonth(ADateTime));
  rowcount := WeekOfTheMonth(EndOfTheMonth(ADateTime)) + 1;

  //set the grid rows and column count
  StringGrid1.ColCount := 8;
  StringGrid1.RowCount := rowcount;
  for I := 1 to 7 do
    StringGrid1.Cells[i, 0] := DayNames[i];

  noofdays := DaysInMonth(ADateTime);
  d := 0;
  rowno := 1;

  while rowno <= rowcount do
  begin
    while colno < 8 do
    begin
      Inc(d);
      StringGrid1.Cells[colno, rowno] := IntToStr(d);
      inc(colno);
      if d = noofdays then
        Exit;
    end;
    colno := 1;
    inc(rowno);
  end;
end;

0
 
LVL 1

Expert Comment

by:peterkiers
ID: 33714353
What about a free component called TJvDays from the JEDI VCL.
It comes with a good example.

Peter

0
 

Author Comment

by:zaferms
ID: 33714973
Thanks ewangoya  for you  Answer
I try it ....
But i have Some Problems
When i Put the date
DrawDatesInGrid(01/03/2010);  March   give me 13 days in the Grid
DrawDatesInGrid(01/05/2010);  May     give me 8 days  in the Grid
DrawDatesInGrid(01/06/2010);  June    give me 12 Days in the Grid
DrawDatesInGrid(01/08/2010);  Aug    give me  14 Days in the Grid
DrawDatesInGrid(01/11/2010);  Nov   give me 13 Days  in the Grid

Plus it will Start on Col1 it should start from col 0

The main Thing  it is not what i want

if you see my code in the Question i need to put GD on stringGrid With in the procedure.  so i donot want to go from scratch again
I JUST WANT TO GET THE GD  OR Picture.Bitmap.Canvas.TextOut on stringGrid..

I Attached project file you can see .

 Thanks again..

Project1.exe
0
 

Author Comment

by:zaferms
ID: 33714985
Thanks peterkiers

But iam trying to do my own.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:peterkiers
ID: 33715168
Oke, I just would like to say:
Why re-event the wheel if it is allread there.

Peter

0
 

Author Comment

by:zaferms
ID: 33715307
[Oke, I just would like to say:
[Why re-event the wheel if it is allread there.]
[Peter]


Thanks peter again

But this is part of project iam working on to make dual calendar
 Gregorian and Hijri  and i Will make it FreeWare  fpr Researchers and instructores on Collages.. Thanks.
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 33716794
try

procedure TForm1.DrawDatesInGrid(ADateTime: TDateTime);
const
  DayNames: array[0..6] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat');
var
  i, noofdays, d, rowno, colno, rowcount: integer;
  firstday: TDateTime;
begin
  colno := DayOfWeek(StartOfTheMonth(ADateTime)) - 1;  //grid starts at 0
  rowcount := ((DaysInMonth(ADateTime) + colno) div 7) + 2;

  //set the grid rows and column count
  StringGrid1.FixedCols := 0;
  StringGrid1.ColCount := 7;
  StringGrid1.RowCount := rowcount;
  for I := 0 to 6 do
    StringGrid1.Cells[i, 0] := DayNames[i];

  noofdays := DaysInMonth(ADateTime);
  d := 0;
  rowno := 1;

  while rowno <= rowcount do
  begin
    while colno <= 6 do
    begin
      Inc(d);
      StringGrid1.Cells[colno, rowno] := IntToStr(d);
      inc(colno);
      if d = noofdays then
        Exit;
    end;
    colno := 0;
    inc(rowno);
  end;
end;
0
 

Author Closing Comment

by:zaferms
ID: 33717516
Thanks ewangoya  for you  Answer
this is improved good

i will give you the points

But Please if yo can help me to control the selected cell on the stringGrid.

i mean ..  i want to move the Blue square to the cell i want

i want it to go to today date for example.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33721851
For coloring or to add a bitmap, use CellCustomDraw of the grid

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DateUtils, ComCtrls, ImgList;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    ImageList1: TImageList;
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    FDateTime: TDateTime;  //selected date so we know what month the calendar is for
    procedure DrawDatesInGrid(ADateTime: TDateTime);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DrawDatesInGrid(Date);
end;

procedure TForm1.DrawDatesInGrid(ADateTime: TDateTime);
const
  DayNames: array[0..6] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat');
var
  i, noofdays, d, rowno, colno, rowcount: integer;
  firstday: TDateTime;
begin
  FDateTime := ADateTime;

  colno := DayOfWeek(StartOfTheMonth(ADateTime)) - 1;  //grid starts at 0
  rowcount := ((DaysInMonth(ADateTime) + colno) div 7) + 2;

  //set the grid rows and column count
  StringGrid1.FixedCols := 0;
  StringGrid1.ColCount := 7;
  StringGrid1.RowCount := rowcount;
  for I := 0 to 6 do
    StringGrid1.Cells[i, 0] := DayNames[i];

  noofdays := DaysInMonth(ADateTime);
  d := 0;
  rowno := 1;

  while rowno <= rowcount do
  begin
    while colno <= 6 do
    begin
      Inc(d);
      StringGrid1.Cells[colno, rowno] := IntToStr(d);
      inc(colno);
      if d = noofdays then
        Exit;
    end;
    colno := 0;
    inc(rowno);
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  dd: Integer;
  SG: TStringGrid;
begin
  SG := Sender as TStringGrid;

  dd := StrToIntDef(SG.Cells[ACol, ARow], 0);
  if dd = 0 then
    Exit;

  if (ACol = 0) and (ARow > 0) then
  begin
    // red for Sunday
    SG.Canvas.Brush.Color := clRed; //your colour
    SG.Canvas.FillRect(Rect);
    // Add the date in white
    SG.Canvas.Font.Color := clWhite;
    SG.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, IntToStr(dd));
  end;

  //need to know if it is todays date
  if (MonthOf(FDateTime) = MonthOf(Date)) and (DayOf(Date) = dd) then
  begin
    // Fill rectangle with your colour
    SG.Canvas.Brush.Color := clSkyBlue;
    SG.Canvas.FillRect(Rect);
    // Next, insert the date
    SG.Canvas.Font.Color := clBlack;
    SG.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, IntToStr(dd));

    {to add an image to the grid, easier way is to use ImageList
    ImageList1.Draw(SG.Canvas, Rect.Left, Rect.Top, dd); //dd will be the image index for that day
    }
  end;
end;

end.
0
 

Author Comment

by:zaferms
ID: 33722779
Thanks alot sir
you been very helpful for me thanks again...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now