Autor Beitrag
D. Annies
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 24.04.08 09:40 
Hi, Delpher,

mit dem folgenden Code greife ich auf ein Stringgrid zu:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
procedure TForm1.SG_x_DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
  State: TGridDrawState);
  procedure WriteText(StringGrid: TStringGrid; ACanvas: TCanvas; const ARect: TRect; const Text: string; Format: Word);
  const  DX = 2;
         DY = 2;
  var S     : array[0..255of Char;
  begin
    with Stringgrid, ACanvas, ARect do
    begin
      case Format of
        DT_LEFT: ExtTextOut(Handle, Left + DX, Top + DY,
            ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);

        DT_RIGHT: ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
            ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text),
            Length(Text), nil);

        DT_CENTER: ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2,
            Top + DY, ETO_OPAQUE or ETO_CLIPPED, @ARect,
            StrPCopy(S, Text), Length(Text), nil);
      end;
    end;
  end;

  procedure Display(StringGrid: TStringGrid; const S: string; Alignment: TAlignment);
  const Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
  begin
    WriteText(StringGrid, StringGrid.Canvas, Rect, S, Formats[Alignment]);
  end;

begin
  if ACol = tntstringgrid1.ColCount-1 then  // in [0..2] then  // hier meine Frage!!
    Display(tntStringGrid1, tntStringGrid1.Cells[ACol, ARow], taRightJustify);

  if ARow = 0 then
    Display(tntStringGrid1, tntStringGrid1.Cells[ACol, ARow], taCenter)
end;


Ich möchte nur rechts formatieren, wenn in der Spalte mit der Nummer ACol ein Preis ausgegeben wird, das kann also in beliebiger Spalte der Stringgrids sein, nicht nur in der letzten Spalte (colcount-1).

Wie kann ich auf den Inhalt, z.B. Spaltenname "Preis" zugreifen? :nixweiss:

Vielen Dank für Hilfe,
Detlef

P.S. habe jetzt den 'hier meine Frage'-Teil etwas überarbeitet:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
  with sender as TStringgrid do
  begin
    if pos('preis', lowercase(cells[Acol,0])) > 0 then
      Display(tntstringgrid1, tntstringgrid1.Cells[ACol, ARow], taRightJustify);
  end;


das klappt auch, aber ich bin auch immer noch von einem bestimmten Stringgrid, hier: tntstringgrid1, abhängig, besse wäre ja, wenn der Code für ein beliebiges Stringgrid gilt.

Gruß, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
Der nette Nachbar
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 67

Win XP, Suse Linux 9.3
Delphi 7, Delphi 2007, Borland C++ Builder 6, Java Builder 7
BeitragVerfasst: Do 24.04.08 10:50 
Anstatt tntstringgrid1, einfach eine Variable übergeben
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 24.04.08 13:16 
Äh, WIE einfach übergeben?
gruß, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Do 24.04.08 14:51 
Dann muss das alles über den Sender gemacht werden. Also überall, wo jetzt explizit tntstringgrid steht : (Sender as TStringGrid).???

Das bedeutet für das zweite Code-Fragment : tntstringgrid.Cells... da muss das tntstringgrid. weg !

_________________
Gruß
Hansa
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 24.04.08 18:33 
Hi, Hansa,

ich freue mich sehr, von dir mal wieder was (eine handlungsanweisende Antwort) zu hören.
Von dir habe ich schon viel gelernt. - Das werde ich gleich mal so codieren.

Herzlichen Dank, ich melde mich wieder,

Gruß, Detlef

_________________
ut vires desint, tamen est laudanda voluntas
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 24.04.08 18:50 
Hi, Hansa,

das klappt soweit gut, nur leider wird das Euro-Zeichen falsch ausgegeben.

Ich habe das Stringgrid gefüllt mit:

ausblenden Delphi-Quelltext
1:
2:
3:
cells[2,z] := format('%8.2f %-2s', [poswert, '€']);
// bzw. mit
cells[2,z] := format('%8.2m', [poswert]);


Beide Formatierungen gehen nicht. :autsch:

Hast du dafür auch noch eine Idee? :flehan:

Bis denne,
Detlef

_________________
ut vires desint, tamen est laudanda voluntas
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Do 24.04.08 19:43 
Steht jetzt überall "Sender" ? Dann ist die Frage ja beantwortet. Zum Rest : verwende am einfachsten so was : Cells [ACol,ARow] := FloatToStrF (wert) + ' €'; Wie das mit dem Format - Monster auch geht : F1. :mrgreen:

_________________
Gruß
Hansa
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Do 24.04.08 19:50 
Hi, Hansa,

danke für dein weiteres Nachdenken - und über dein grünes "Monster" habe ich mich auch wieder gefreut ... (ehrlich)

Gruß,
Detlef

_________________
ut vires desint, tamen est laudanda voluntas