Autor Beitrag
Goaznic
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23

Win XP, Win 7
Delphi 5, Delphi 10
BeitragVerfasst: Mi 21.09.11 11:17 
Hey. :)

Ich hab folgende Prozedur für das Drucken eines StringGrids:

ausblenden 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:
procedure TFormMain.Crossworddrucken1Click(Sender: TObject); // Drucken
var
  F: TextFile;
  s, oldFnt: string;
  I,e: word;
begin
  Printer.PrinterIndex := -1// Standarddrucker einstellen
  oldFnt := Printer.Canvas.Font.Name; // Druckerfont merken
  Printer.Canvas.Font.Name := 'Coutier New';
  AssignPrn(F); // Textfile mit Drucker verbinden
  try
    Rewrite(F); // Textfile initialisieren
    with grdGrid do
    begin
      for e := 0 to ColCount - 1 do
        for I := 0 to RowCount - 1 do
        begin
          s := Format('%-1s', [Cells[e, I]]);
          Writeln(F, s);
        end
    end
    finally
      CloseFile(F); // Textdatei schließen
      Printer.Canvas.Font.Name := oldFnt; // Druckerfont zurücksetzen
    end;
  end;


Problem: Das Grid wird nicht wie ein Grid gedruckt, sondern alle Spalten werden direkt untereinander ausgedruckt.
Bsp.:

1|5|3|6
9|7|2|1
4|4|5|3
3|4|7|8

wird so gedruckt:

1
9
4
3
5
7
4
4
3
2
5
7
6
1
3
8
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mi 21.09.11 11:28 
user profile iconGoaznic hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
          Writeln(F, s);					




Kleiner Tip: Für was steht denn das "ln" in Writeln? ;-)

_________________
Delphi: XE - OS: Windows 7 Professional x64
Goaznic Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23

Win XP, Win 7
Delphi 5, Delphi 10
BeitragVerfasst: Mi 21.09.11 11:38 
keine Ahnung. Für Line? :'D
Jedenfalls schreibt er mir die spalten nebeneinander in einer zeile, wenn ich das ln weg lasse ...
und untereinander in einer spalte, wenn ich es verwende.
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mi 21.09.11 12:45 
Genau richtig!
Write schreibt einfach hintereinander Text.
Writeln (ln = line) schreibt einen Text und führt dann einen Zeilenumbruch aus.

Um dein Gitter auszugeben musst du nun einfach erst einmal die Zeichen einer Zeile ausgeben und dann einen Zeilenumbruch.


ausblenden Pseudocode
1:
2:
3:
4:
5:
für alle y tue
  für alle x in y tue 
      drucke zeichen[x,y]
      wenn x = letzte spalte dann
        drucke zeilenumbruch


Wahlweise kannst du nach jedem Zeichen noch ein Trennzeichen drucken und so weiter und so fort.



edit: typo

_________________
Delphi: XE - OS: Windows 7 Professional x64


Zuletzt bearbeitet von Horschdware am Mi 21.09.11 13:13, insgesamt 1-mal bearbeitet

Für diesen Beitrag haben gedankt: Goaznic
Goaznic Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23

Win XP, Win 7
Delphi 5, Delphi 10
BeitragVerfasst: Mi 21.09.11 13:04 
Also müsste das dann quasi so aussehen?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
      for I := 0 to RowCount - 1 do
        for e := 0 to ColCount - 1 do
        begin
          s := Format('%-1s', [Cells[e, I]]);
          Write(F, s);
          if e = ColCount - 1 then
            writeln(F, s);
        end
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mi 21.09.11 13:11 
Fast.
Die letzte Spalte würde nun immer zwei mal ausgegeben werden, da writeln ja nicht NUR einen Zeilenumbruch schreibt, sondern Text + Zeilenumbruch, du aber bereits davor das Zeichen ausgegeben hast.
Schiebe dein erstes "Write" unten in den Else-Zweig oder gib dem Wirteln dein Zeichen in s nicht mit.

Btw: Probiers doch einfach mal aus ;-)

_________________
Delphi: XE - OS: Windows 7 Professional x64

Für diesen Beitrag haben gedankt: Goaznic
Goaznic Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23

Win XP, Win 7
Delphi 5, Delphi 10
BeitragVerfasst: Mi 21.09.11 13:17 
Oh ja, jetzt funktioniert's. :)
Danke. ;)