Autor Beitrag
klezmor
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Do 13.03.08 18:20 
Ich habe mich zwar schon um die Forensuche bemüht, bin daraus, allerdings irgendwie nicht schlau geworden.
ich habe ein TListview mit einer Columne, welche Nr. heißt und die einfach nur fortlaufend nach unten durchnummeriert ist. Nun möchte ich zur Laufzeit 2 neue neue Columns(Spalten) hinzufügen( mein style ist vsReport).Wenn die columns existieren möchte ich diese mit Inhalt füllen wobei ich keine Ahnung mit der Indexierung habe und wie ich dann auf ein einzelnes Element(denke mal das wird dann ein item sein) zugreifen kann.

MfG Klezmor.

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 13.03.08 19:50 
Hallo user profile iconklezmor,

Du kannst mit ListView1.Columns auf die ganzen Columns zugreifen. Verwende folgende Methoden / Eigenschaften, um Spalten zu hinzuzfügen, verändern, zu löschen usw...:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
ListView1.Columns.Add;           // Fügt eine neue Spalte ein und gibt sie zurück
ListView1.Columns.Delete(Index); // Löscht die Spalte Index
ListView1.Columns.Clear;         // Löscht alle Spalten;
ListView1.Columns.Insert(Index); // Fügt vor Stelle Index eine neue Spalte ein und gibt sie zurück

ListView1.Columns.Items[Index];  // Greift auf Element Index zu
ListView1.Columns.Count;         // Gibt die Anzahl der Elemente zurück

Verwende folgende Eigenschaften eines Columns, um es zu bearbeiten:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
Caption     // Anzeigename der Spalte
Alignment   // Ausrichtung von Caption (taLeftJustify, taCenter, taRightJustify)
Width       // Breite der Spalte
AutoSize    // Width automatisch an die Breite von Caption anpassen?

Ich habe mir mal zwei kleine Prozeduren geschrieben, die diese Spalten in einen String auslesen und aus ihm wieder setzen können.

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:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
procedure TForm1.SetColumns(Columns: String);
var i, W: Integer; C, Tmp: String; A: TAlignment; AlignSetted: Boolean;
begin
  if Columns = OldColumns then Exit;         // Wenn keine Änderung, Prozedur verlassen
  OldColumns := Columns;
  ListView.Columns.Clear;                    // Columns leeren
  i := 0;                                    // Variablen initialisieren
  AlignSetted := False;
  W := 0;
  while i < Length(Columns) do               // Jedes Zeichen abklappern
  begin
    case Columns[i+1] of                     // Zeichen prüfen
      ':':                                   // Ende Name; Anfang Länge
      begin
        C := Tmp;
        Tmp := '';
      end;
      '?':                                   // Ende Länge; Anfang Ausrichtung
      begin
        AlignSetted := True;       
        W := StrToInt(Tmp);
        Tmp := '';
      end;
      '&':                                   // Ende Länge/Ausrichtung; Anfang Nächtes
      begin
        A := taLeftJustify;
        if AlignSetted then                  // Wenn Ausrichtung gesetzt, Ausrichtung anwenden
        begin
          if Tmp = 'right' then A := taRightJustify;
          if Tmp = 'center' then A := taCenter;
        end
        else
          W := StrToInt(Tmp);                // Sonst Breite setzen
        Tmp := '';
        with ListView.Columns.Add do         // Element erzeugen
        begin
          Caption := C;                      // Name setzen
          Width := W;                        // Breite setzen
          Alignment := A;                    // Ausrichtung setzen
        end;
        C := '';                             // Werte leeren
        W := 0;
        AlignSetted := False;
      end;
      else
      begin
        Tmp := Tmp + Columns[i+1];           // Wenn kein Steuerzeichen, Zeichen anfügen
      end;
    end;
    inc(i);
  end;       
  A := taLeftJustify;                        // Letzes Element einfügen
  if AlignSetted then
  begin
    if Tmp = 'right' then A := taRightJustify;
    if Tmp = 'center' then A := taCenter;
  end
  else
    W := StrToInt(Tmp);
  Tmp := '';
  with ListView.Columns.Add do
  begin
    Caption := C;
    Width := W;
    Alignment := A;
  end;
end;

function TForm1.GetColumns: String;
var i: Integer;
begin
  for i := 0 to ListView.Columns.Count-1 do  // Columns abklappern
  begin
    Result := Result + ListView.Columns[i].Caption+':'+ // Name und Breite schreiben
      IntToStr(ListView.Columns[i].Width);
    if ListView.Columns[i].Alignment = taCenter then // Ausrichtung ggf. schreiben
      Result := Result + '?center';
    if ListView.Columns[i].Alignment = taRightJustify then
      Result := Result + '?right';
    if i < ListView.Columns.Count-1 then Result := Result+'&'; // Steuerzeichen anfügen
  end;
end;

Das hier ist ein Beispiel für so eine String-Column-Liste:

ausblenden Quelltext
1:
Titel:150&Interpret:150&Album:100&Länge:100?right&Dateiname:250					

Also ergibt sich folgendes Schema:

ausblenden Quelltext
1:
Anzeigename:Breite?Ausrichtung&Anzeigename2:Breit?Ausrichtung2 ...					

Für Ausrichtung gelten left, right, center.

Bitte versuche auch, den Code zu verstehen. Nur kopieren bringt dir nix!

Grüße,
Yogu
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Do 13.03.08 20:28 
Also was du mir mit dem code sagen willst habe ich ncoh nicht ganz verstanden, aber werde es mir aufjedenfall noch genauer anschauen.
Hätte allerdings noch eine weitere frage wie füge ich einfach einen string den columns hinzu.
also praktisch links oben rechts oben, dann nächste reihe links dann wieder rehcts und immer so weiter am besten nur mit einem Befehl und wie greife ich auf ein bestimmtes item zu. z.b. 2 spalte 5. reihe oder so.

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 13.03.08 20:35 
user profile iconklezmor hat folgendes geschrieben:
Also was du mir mit dem code sagen willst habe ich ncoh nicht ganz verstanden

Ich wollte deine Frage beantworten ;)

user profile iconklezmor hat folgendes geschrieben:
Hätte allerdings noch eine weitere frage wie füge ich einfach einen string den columns hinzu. also praktisch links oben rechts oben, dann nächste reihe links dann wieder rehcts und immer so weiter am besten nur mit einem Befehl und wie greife ich auf ein bestimmtes item zu. z.b. 2 spalte 5. reihe oder so.

Ich glaube, du verwechselst da etwas. ListView ist dazu da, Zeilen anzuzeigen. Diese Zeilen können noch bestimmte Eigenschaften haben, also im Explorer die Dateigöße, der Dateityp usw. Ich glaube allerdings, dass du eher eine Tabelle machen willst. Dazu ist TStringGrid da. Schaue dir mal diese Komponente und die Delphi Hilfeseite dazu mal an.
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Do 13.03.08 21:10 
Was mir an dem Stringrid nicht gefällt, ist dass die grauen bereiche so dick sind und links am rand auch solche grauen balken sind, welche ich nciht brauche.

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Regan
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2157
Erhaltene Danke: 72


Java (Eclipse), Python (Sublimetext 3)
BeitragVerfasst: Do 13.03.08 21:12 
Schonmal FixedColumns und FixedRows auf 0 getstellt?
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Do 13.03.08 21:54 
Gibt es keine Komponente von der Art einer Tabelle, welche das Aussehen von einem TListview hat?

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 13.03.08 21:59 
user profile iconklezmor hat folgendes geschrieben:
Gibt es keine Komponente von der Art einer Tabelle, welche das Aussehen von einem TListview hat?

user profile iconklezmor hat folgendes geschrieben:
Was mir an dem Stringrid nicht gefällt, ist dass die grauen bereiche so dick sind und links am rand auch solche grauen balken sind, welche ich nciht brauche.

user profile iconRegan hat folgendes geschrieben:
Schonmal FixedColumns und FixedRows auf 0 getstellt?

:les: :think: :idea: ;)
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Do 13.03.08 22:06 
Das schreibe ich ja, weil das stringgrid ganz anders aussieht. :!:
Ich meine den xpstyle für das Listview.

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Fr 14.03.08 15:00 
Oh, tut mir leid. Daran habe ich nicht gedacht, ich verwende Win2000. Leider habe ich keine Ahnung, ob es ein Control gibt, das Tabellen verwaltet und an Windows XP angepasst ist.
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Fr 14.03.08 15:59 
Hab jetzt ein Stringgrid genommen, wenns auch nicht ganz so modern aussieht.

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth