Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

trying to Put  Calendar days no  in stringGrid

Posted on 2010-09-19
11
Medium Priority
?
1,633 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:Ephraim Wangoya
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:Ephraim Wangoya
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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:
Ephraim Wangoya earned 750 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:Ephraim Wangoya
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Screencast - Getting to Know the Pipeline

885 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