J'utilise une StringGrid dans laquelle je voudrais utiliser un Mask de date dans la 1ere colonne.
pour cela dans l'événement "OnGetEditMask" de la StringGrid, je mets :
if ACol=0 then begin Value:='99/99/2099'; // mois/jour/année end;
Ma première question est la suivante : pourquoi en utilisant ce masque, j'ai l'affichage suivant dans la cellule :"__/__/2___" au lieu de "__/__/20__" ?
Ensuite, je voudrais faire en sorte que la personne qui entre la date ne puisse pas entrer quelque chose du style "88/88/2888", donc paramétrer
- le 1er chiffre
pour des valeurs 0..1 si le 2eme [0..2]
pour des valeurs 0 si le 2eme [3..9]
....
donc j'utilise la fonction "OnKeyPress", avec ce code
var MyTEXT : String; ed: TInplaceEdit; MyValue : Integer; begin if (MyCOL=0) then begin MyTEXT:=REVISION_LISTBOX.Cells[MyCOL,MyROW]; ed:= GetInPlaceEdit(REVISION_LISTBOX); if ed <> nil then begin case ed.SelStart of //mois 0 : begin case StrToIntDef(MyTEXT[2], 0) of 0..2 : if not (key in ['0','1']) then key:=#0; 3..9 : if not (key = '0') then key:=#0; -1 : if not (key in ['0'..'9']) then key:=#0; end; end; //jour //annee end; end; end;
Mais lorsque le 2eme chiffre du mois n'est pas indique (vide), j'ai un "acces violation" alors que cela se passe bien s'il est indique.
Ma question est donc : a quoi correspond le vide dans le mask si "StrToIntDef" ne s'en sort pas ?
Je semble avoir trouvé une solution pour le 2eme point :
- initialiser la cellule a tester a sa création par "00/00/0000"
en cliquant dessus pour l'éditer cela affiche : "00/00/2___"
ensuite le code suivant fonctionne, apparemment :
sur le onkeypress de la sringrid
var MyTEXT : String; ed: TInplaceEdit; MyValue : Integer; begin MyTEXT:=REVISION_LISTBOX.Cells[MyCOL,MyROW];
ed:= GetInPlaceEdit(REVISION_LISTBOX); if ed <> nil then begin case ed.SelStart of //mois 0 : begin case StrToIntDef(MyTEXT[2], 0) of 0..2 : if not (key in ['0','1']) then key:=#0; 3..9 : if not (key = '0') then key:=#0; -1 : if not (key in ['0'..'9']) then key:=#0; end; end; 1 : begin case StrToIntDef(MyTEXT[1], 0) of 0 : if not (key in ['0'..'9']) then key:=#0; 1 : if not (key in ['0'..'2']) then key:=#0; -1 : if not (key in ['0'..'9']) then key:=#0; end; end; //jour 3 : begin case StrToIntDef(MyTEXT[5], 0) of 0..1 : if not (key in ['0'..'3']) then key:=#0; 2..9 : if not (key in ['0'..'2']) then key:=#0; -1 : if not (key in ['0'..'3']) then key:=#0; end; end; 4 : begin case StrToIntDef(MyTEXT[4], 0) of 0..2 : if not (key in ['0'..'9']) then key:=#0; 3 : if not (key in ['0'..'1']) then key:=#0; -1 : if not (key in ['0'..'9']) then key:=#0; end; end; //annee 7 : key:='0'; end; end; end;
la solution vient de l'initialisation de la cellule.
Si elle ne l'est pas, la cellule avec son mask (__/__/2___) a une longueur nulle et donc StrToIntDef ne passe pas
Concernant le premier point je suis toujours a la recherche d'une solution :
" pourquoi en utilisant ce masque (
Value:='99/99/2099'; // mois/jour/année
), j'ai l'affichage suivant dans la cellule :"__/__/2___" au lieu de "__/__/20__" ? "
Bonjour,
Pourquoi, beaucoup plus simple, n'utiliserais-tu pas un tDatetimepicker qui s'afficherait dans la cellule de ton stringgrid ? Regarde il y a plein d'exemples sur le net, ici c'est dans un dbgrid mais le principe est le même, et çà marche super bien.
procedure TForm1.DateTimePickerExit(Sender: TObject); begin MyGRID.Cells[MyCOL, MyROW]:=FormatDateTime('mm/dd/yyyy', DateTimePicker.Date); end;
procedure TForm1.FormCreate(Sender: TObject); begin DateTimePicker.Date:=Now; end;
procedure TForm1.MyGRIDDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var txt : string; I : Integer; begin With Sender As TStringGrid Do With Canvas Do Begin DateTimePicker.Visible:=False; if (ACol <> 0) then begin FillRect(rect); end else begin if (gdFocused in State) then begin with DateTimePicker do begin Left := Rect.Left + MyGRID.Left + 1; Top := Rect.Top + MyGRID.Top + 1; Width := Rect.Right - Rect.Left + 2; Width := Rect.Right - Rect.Left + 2; Height := Rect.Bottom - Rect.Top + 2;
Visible := True; end; MyCOL:=ACol; MyROW:=ARow; end; FillRect(rect); End; DrawText(Canvas.Handle,PChar(Cells[ACol,ARow]),-1,Rect,DT_CENTER or DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE ); end; end;
procedure TForm1.MyGRIDSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); begin if (ACol <> 0) then DateTimePicker.Visible:=False else DateTimePicker.Visible:=True; end;