trying to Put Calendar days no in stringGrid

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


zafermsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ephraim WangoyaConnect With a Mentor Commented:
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
 
Ephraim WangoyaCommented:
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
 
Ephraim WangoyaCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
peterkiersCommented:
What about a free component called TJvDays from the JEDI VCL.
It comes with a good example.

Peter

0
 
zafermsAuthor Commented:
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
 
zafermsAuthor Commented:
Thanks peterkiers

But iam trying to do my own.
0
 
peterkiersCommented:
Oke, I just would like to say:
Why re-event the wheel if it is allread there.

Peter

0
 
zafermsAuthor Commented:
[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
 
zafermsAuthor Commented:
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
 
Ephraim WangoyaCommented:
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
 
zafermsAuthor Commented:
Thanks alot sir
you been very helpful for me thanks again...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.