Autor Beitrag
bayou
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 88


D6 PE
BeitragVerfasst: Fr 19.05.06 21:33 
Hi,

erstmal hoffe ich hier mit meiner Frage richtig zu sein, sonst bitte verschieben. Danke

wie bekomme ich denn den längsten Eintrag einer Listbox heraus?
Ich benötige das um die Formulargröße anzupassen.

Ich hatte es mal so versucht.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
if (FormSubFigure.SubFigureCheckList.Count > 0) then begin
  for k := 0 to FormSubFigure.SubFigureCheckList.Items.Count -1 do
    FormSubFigure.Width := FormSubFigure.Canvas.TextWidth(FormSubFigure.SubFigureCheckList.Items.Strings[k]) + 10;
  FormSubFigure.Height := FormSubFigure.SubFigureCheckList.ItemHeight*(FormSubFigure.SubFigureCheckList.Count +1)//;
end
else begin
  FormSubFigure.Height := FormSubFigure.SubFigureCheckList.ItemHeight * 2;
  FormSubFigure.Width := 400;
end;


so geht es aber leider nicht.
Vor allem wird ansch. immer die Länge des letzten Eintrages verwendet.

schon mal Vielen Dank
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 19.05.06 23:20 
Hallo,

in etwa so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var i,max,maxIndex,temp : Integer;
begin
  max := 0;
  for i := 0 to ListBox1.Items.Count -1 do
    begin
    temp := ListBox1.Canvas.TextWidth(ListBox1.Items[i]);
    if temp > max then
      begin
      max := temp;
      maxIndex := i;
      end;
    end;
  showmessage('ItemIndex: '+IntToStr(maxIndex)+'  Pixel: '+IntToStr(max));
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
bayou Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 88


D6 PE
BeitragVerfasst: Sa 20.05.06 00:45 
Super, vielen Dank :D

Soweit funktioniert es sehr gut.

allerdings habe ich noch ein Problem.

Wenn ich das Formular mit meinem Menü öffne ist das Formular zu klein.
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 TFormMain.Subfigure1Click(Sender: TObject);
var
  i, j, k, max, temp: Integer;
begin
  i := FigureCheckList.ItemIndex; // aktuell ausgewähltes Item bestimmen
  if (i > -1) then begin // ist was ausgewählt? -> ja
    CurrentFigure := TFigure(FigureCheckList.Items.Objects[i]);
    FormSubFigure.SubFigureCheckList.Items.Assign(CurrentFigure.Subfigure);

    for j := 0 to FormSubFigure.SubFigureCheckList.Count-1 do
      FormSubFigure.SubFigureCheckList.Checked[j] := (TFigureState(CurrentFigure.Subfigure.Objects[j]) <> fsNotStarted);

    FormSubFigure.Caption := 'SubFigure  ' + CurrentFigure.Name;
    if (FormSubFigure.SubFigureCheckList.Count > 0) then begin
      max := 0;
      for k := 0 to FormSubFigure.SubFigureCheckList.Items.Count -1 do begin
        temp := FormSubFigure.SubFigureCheckList.Canvas.TextWidth(FormSubFigure.SubFigureCheckList.Items[k]);
        if temp > max then begin
          max := temp;
        end;
      end;
      FormSubFigure.Width := max;
      FormSubFigure.Height := FormSubFigure.SubFigureCheckList.ItemHeight*(FormSubFigure.SubFigureCheckList.Count +2)//;
    end
    else begin
      FormSubFigure.Height := FormSubFigure.SubFigureCheckList.ItemHeight * 2;
      FormSubFigure.Width := 600;
    end;

    FormSubFigure.Show;
  end
  else begin // nix ausgewählt, Referenzen entfernen und Liste leeren
    ShowMessage(SelectFigure);
    FormSubFigure.SubFigureCheckList.Clear;
    CurrentFigure := NIL;
  end;
end;


wenn das Formular geöffnet ist und ich über OnClick
ausblenden Delphi-Quelltext
1:
2:
3:
4:
    if (FormSubFigure.Visible = true) then begin
              FormSubFigure.SubFigureCheckList.Clear;
              Subfigure1Click(Sender);
    end;

mache funktioniert es

kann mir da noch jemand einen Tip geben?


Zuletzt bearbeitet von bayou am Sa 20.05.06 00:49, insgesamt 1-mal bearbeitet
aim65
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 312

Win 9x, Win XP
Delphi 3pro, 7PE
BeitragVerfasst: Sa 20.05.06 10:37 
Fällt mir gerade ein:
Kannst du den Anpassungskram nicht in das "FormSubFigure.Show oder -Activate" Event reinpacken? Keine Ahnung, ob das bei dir ginge.

Rein kosmetisch: ich würde (vor Zeile 8) "with FormSubFigure do..." arbeiten. Erspart ne Menge Tipparbeit. :wink:

Edit: schon wieder der Mist mit den Smilie codes: muß natürlich "Zeile 8" heißen.
bayou Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 88


D6 PE
BeitragVerfasst: Sa 20.05.06 19:15 
Erstmal vielen Dank für deine Antwort.

Ich habe die Sache jetzt in OnShow und OnActivate versucht, habe aber immer noch das Problem wie oben beschrieben.