Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.149 Beiträge
 
Delphi 11 Alexandria
 
#1

KI Code verbessern, ListBox mit Label

  Alt 28. Jul 2025, 10:47
Ich habe mir über KI eine Funktion zum Erstellen einer ListBox mit einer Überschrift zusammengestellt:

Delphi-Quellcode:

function CreateCheckListBox(AParent: TWinControl; Top, Left, W, H: Integer;
  Titel: String): TListBox;
var
  ListBoxLabel: TLabel;
  aListBox: TListBox;
begin
  // Create the TListBox component
  aListBox := TListBox.Create(AParent);
  try
    aListBox.Parent := AParent;
    // Position and size the list box
    aListBox.SetBounds(Left, Top, W, H);

    // Create a label for the list box
    ListBoxLabel := TLabel.Create(AParent);
    ListBoxLabel.Parent := AParent;
    // Position label relative to the list box
    ListBoxLabel.SetBounds(Left, Top - 25, W, 20); // Positioned 25 pixels above the list box
    ListBoxLabel.Caption := Titel;

    // Return the created list box
    Result := aListBox;
  except
    aListBox.Free;
    raise;
  end;
end;

Meine Listboxen erzeuge ich dann zur Luufzeit auf meinem Form mit dieser Zeile Code:

Delphi-Quellcode:
  

// Create the fifth list box
  lst_Elements := CreateCheckListBox(PanelListBoxes, StartTop,
    CurrentLeft, ListBoxWidth, ListBoxHeight, 'Show Elements');
  inc(CurrentLeft, ListBoxWidth + SpacingX);


Funktioniert soweit ganz gut, über einige weitere Iterationen habe ich dann diese Funktion erhalten um meine 6 List Boxen auf meinem Form auch bei einer Größenanpassung zu "Resizen" :




Delphi-Quellcode:
procedure MyForm.ResizeListBoxes;
const
  SpacingX = 20; // Space between list boxes horizontally
  StartTop = 30; // Starting position from the top of the container
  StartLeft = 10; // Starting position from the left of the container
var
  i: Integer;
  CurrentLeft: Integer;
  ListBoxWidth, ListBoxHeight: Integer;
  ListBoxes: array[0..5] of TWinControl; // Array for both TListBox and TCheckListBox
begin
  // Initialize the list boxes array
  ListBoxes[0] := dlist; // ...
  ListBoxes[1] := SelectedIndices; // ...
  ListBoxes[2] := lst_List; // TListBox
  ListBoxes[3] := lst_List2; // TListBox
  ListBoxes[4] := lst_Elements; // TListBox
  ListBoxes[5] := lst_List3; // TListBox

  // Initialize starting left position
  CurrentLeft := StartLeft;

  // Calculate dimensions
  ListBoxWidth := Round((PanelListBoxes.ClientWidth - ((Length(ListBoxes) - 1) * SpacingX)) /
    Length(ListBoxes)); // Divide width evenly among list boxes
  ListBoxHeight := PanelListBoxes.ClientHeight - 2 * StartTop;

  // Adjust all list boxes dynamically
  for i := Low(ListBoxes) to High(ListBoxes) do
  begin
    if Assigned(ListBoxes[i]) then
    begin
      // Set bounds for the list box or checklist box
      ListBoxes[i].SetBounds(CurrentLeft, StartTop, ListBoxWidth, ListBoxHeight);
      // Move to the next position
      Inc(CurrentLeft, ListBoxWidth + SpacingX);
    end;
  end;
end;
Wo meine KI an Ihre aktuellen Grenzen Stößt: Label und Listbox bei einer Resize-Änderung anzupassen. Was ist denn hier der einfachste Weg?
  Mit Zitat antworten Zitat