Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

trying to Put  Calendar days no  in stringGrid

Posted on 2010-09-19
11
1,510 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Controlled Assessment GCSE - desperate help needed 4 102
Firemonkey android show image from resource ? 1 46
update joined tables 2 55
Firemonkey allowing RTL on android 6 47
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

829 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