Autor Beitrag
Lokke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Mo 25.04.05 12:32 
Hi Leute,

hab hier rumgeschaut aber das passende für mich nicht gefunden. Ich habe eine StringGrid (2 Spalten und 7 Reihen) Komponente und möchte das sich die Spaltenbreite von Row/Col sich automatisch der Textlänge anpasst.

Hab auch schon was zusammen aber irgendwie klappt es nicht, wo ist der Fehler??

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:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Options := StringGrid1.Options + [goEditing] + [goTabs];
end;


procedure grdColWidth(grd: TStringGrid; min: Word);
var i, j, tmp, max: Word;
const dx = 5;

begin
with grd do begin
for j := 0 to (ColCount -1) do begin // alle Spalten
max := 0;
for i := 0 to (RowCount - 1 ) do begin // alle Zeilen
tmp := Canvas.TextWidth(Cells[j, i]);
if tmp > max then max := tmp
end;
if max > min then // nur wenn Minimalbreite überschritten wird,
ColWidths[j] := max + GridLineWidth + dx // ... dann Spalte verbreitern
end
end
end;


procedure TForm1.TabSheet2Show(Sender: TObject);
begin
  Weiter3.Enabled := false;
  
  grdColWidth(StringGrid1, 30);

  StringGrid1.Cells [0,0] := 'Solldruck';
  StringGrid1.Cells [0,1] := 'Angestrebter Druckunterschied';
  StringGrid1.Cells [0,2] := 'notwendige Druckunterschied';  
  StringGrid1.Cells [0,3] := 'Angestrebter Offsetdruck';
  StringGrid1.Cells [0,4] := 'Startspannung Amplitude';
  StringGrid1.Cells [0,5] := 'Startspannung Offsetdruck';
  StringGrid1.Cells [0,6] := 'Erlaubte Befüllung pro Stunde';

  StringGrid1.Cells [1,0] := SolldruckKammer.Text;
  StringGrid1.Cells [1,1] := angestrebteDeltaDruck.Text;
  StringGrid1.Cells [1,2] := notwendigeDeltaDruck.Text;
  StringGrid1.Cells [1,3] := angestrebteOffsetdruck.Text;
  StringGrid1.Cells [1,4] := StartSpgAmpl.Text;
  StringGrid1.Cells [1,5] := StartspgOffsetdruck.Text;
  StringGrid1.Cells [1,6] := erlaubteBefuellung.Text;
end;


Danke
Lokke Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Mo 25.04.05 15:21 
Ich hab mein Problem inzwischen selber gelöst,

hier der Code, um die Spaltenbreite von StringGrid automatisch dem eingegebenen Text anzupassen:

Aufruf in einer Ereignis mit:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.FormShow(Sender: TObject);
begin
   Spaltenbreite(StringGrid1, 2, 5);  //Spaltenbreite von StringGrid1, Spalte=2, Offset=5
end;


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure Spaltenbreite(Grid: TStringGrid; Spalten, offset: integer);
var
  z: integer; // Zeilen
  Spaltenbreite: integer;
begin
  Spaltenbreite := 0;
  with Grid do 
  begin
   for z := 0 to RowCount-1 do 
   begin
    if Canvas.TextWidth(Cells[Spalten, z]) > Spaltenbreite then
    Spaltenbreite := Canvas.TextWidth(Cells[Spalten, z]);    //neue Spaltenbreite
   end;
   ColWidths[Spalten] := Spaltenbreite + offset;   // offset =der Text wird nicht ganz linksbündig angezeigt, deshalb wird der rechte Rand verschoben

  end;
end;