Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   KI Code verbessern, ListBox mit Label (https://www.delphipraxis.net/217591-ki-code-verbessern-listbox-mit-label.html)

bernhard_LA 28. Jul 2025 10:47

KI Code verbessern, ListBox mit Label
 
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?

Uwe Raabe 28. Jul 2025 11:12

AW: KI Code verbessern, ListBox mit Label
 
Sind die Listboxen alle nebeneinander oder in Gitter angeordnet? Dann würde ich ein TGridPanel empfehlen. Mit den passenden Einstellungen macht das den gesamten Code obsolet.

skybibo 28. Jul 2025 11:29

AW: KI Code verbessern, ListBox mit Label
 
Ich frage mich auch, ob man wirklich für so ein paar Zeilen die KI verwenden muss. Diese paar Zeilen sollte jeder Programmierer im Schlaf können. Ich glaube auch nicht, dass es mit der KI schneller geht. Ich muss ja erst die Frage richtig stellen, den Code kopieren und prüfen, ob die KI alles sinnvoll gemacht hat.

bernhard_LA 28. Jul 2025 12:27

AW: KI Code verbessern, ListBox mit Label
 
Ich hätte den Code auch selbst hinbekommen – intellektuell ist da nichts Kompliziertes enthalten. Die KI ist hier eine Art Schreibmaschine, die meine Ideen und Entwürfe schnell, ohne Tippfehler und direkt für Copy & Paste bereitstellt. Darin sehe ich durchaus eine Zeitersparnis.


Ich hätte jedoch erwartet, dass die KI mir irgendwann im Verlauf meiner Diskussion eine Komponente wie eine ListBox und ein Label aufbaut oder eine andere, bessere Lösung wie ein GridPanel vorschlägt.


Vermutlich ist so etwas aktuell mit ChatGPT-4 noch nicht machbar. Mal sehen, was mit ChatGPT-5 möglich sein wird.

bernhard_LA 28. Jul 2025 12:32

AW: KI Code verbessern, ListBox mit Label
 
PS: für das resize Thema wurde jetzt eine "TAG" Variante erstellt, nicht schön aber funktional


Delphi-Quellcode:
procedure TLibElementConnectionsForm.ResizeListBoxes;
const
  SpacingX = 20; // Space between list boxes horizontally
  LabelHeight = 20; // Height of the labels
  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 TCheckListBoxEXT
  Labels: array[0..5] of TLabel;        // Array to store corresponding labels
begin
  // Initialize the list boxes array
  ListBoxes[0] := device_list;               // TCheckListBoxEXT
  ListBoxes[1] := ...                    // TCheckListBoxEXT
  ListBoxes[2] := ...                   // TListBox
  ListBoxes[3] := ...                        // TListBox
  ListBoxes[4] := ..      ;              // TListBox
  ListBoxes[5] := lst_List;              // TListBox

  // Initialize the labels array (you must ensure these labels are created and assigned in your creation code)
  Labels[0] := TLabel(device_list.Tag);               // Assuming the label is stored in Tag property
  Labels[1] := TLabel(SelectedIndices.Tag);
  Labels[2] := TLabel(....Tag);
  Labels[3] := TLabel(....Tag);
  Labels[4] := TLabel(...);
  Labels[5] := TLabel(lst_List.Tag);

  // 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 - LabelHeight; // Adjust height to leave space for the labels

  // Adjust all list boxes and their labels dynamically
  for i := Low(ListBoxes) to High(ListBoxes) do
  begin
    if Assigned(ListBoxes[i]) then
    begin
      // Set bounds for the label above the list box
      if Assigned(Labels[i]) then
      begin
        Labels[i].SetBounds(CurrentLeft, StartTop - LabelHeight, ListBoxWidth, LabelHeight);
      end;

      // 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;




Delphi-Quellcode:
function CreateCheckListBox(AParent: TWinControl; Top, Left, W, H: Integer;
  Titel: String): TCheckListBox;
var
  ListBoxLabel: TLabel;
begin
  // Create the TCheckListBox component
  Result := TCheckListBox.Create(AParent);
  try
    Result.Parent := AParent;
    Result.SetBounds(Left, Top, W, H);
    Result.Items.Clear;

    // Create a label for the checklist box
    ListBoxLabel := TLabel.Create(AParent);
    ListBoxLabel.Parent := AParent;
    ListBoxLabel.Top := Top - 20; // Position label slightly above the list box
    ListBoxLabel.Left := Left;
    ListBoxLabel.Caption := Titel;

    // Store the label in the Tag property of the list box
    Result.Tag := Integer(ListBoxLabel);
  except
    Result.Free;
    raise;
  end;
end;

bernhard_LA 28. Jul 2025 14:55

AW: KI Code verbessern, ListBox mit Label
 
(update asap)

skybibo 28. Jul 2025 20:57

AW: KI Code verbessern, ListBox mit Label
 
Die Lösung mt dem Tag finde ich nicht passend. Auch die Konstanten würde ich in eine separate Unit auslagern. Eleganter wäre hier die Verwendung von Frames oder ein neues Control TLabelListBox. Der Aufwand wird sich jedoch nur lohnen, wenn dies sehr oft benötigt wird. Wie Uwe schon mitgeteilt hat, wird das GridPanel optimal sein. Während dein Weg unter 50 Zeilen kaum machbar ist, benötigt man mit dem GridPanel keine einzige Zeile.

bernhard_LA 28. Jul 2025 21:47

AW: KI Code verbessern, ListBox mit Label
 
eine TLabellistbox habe ich nach einigen weiteren Iterationen/Diskussionen auch bekommen:
Delphi-Quellcode:

interface

uses
  math,
  System.SysUtils,
  System.Classes,
  VCL.Controls,
  VCL.StdCtrls;

type

  [ComponentPlatformsAttribute(pidWin32 or pidWin64)]
  TListBoxEXT = class(TListBox)
  private
    FLabel: TLabel; // Label associated with the ListBox

    FDeltaY: Integer;
  public
    constructor Create(AOwner: TComponent); overload; // Default constructor

    constructor Create(AOwner: TComponent; const LabelName: String;
      XPos, YPos: Integer); overload; // Overloaded constructor

    constructor Create(AOwner: TComponent; const LabelName: String;
      XPos, YPos, Width, Height: Integer); overload;

    destructor Destroy; override;

    // Accessor for the associated label
    property LabelControl: TLabel read FLabel;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TOOLS', [TListBoxEXT]);
end;

{ TListBoxEXT }

constructor TListBoxEXT.Create(AOwner: TComponent);
begin
  inherited Create(AOwner); // Call the base class constructor

  self.Parent := TWinControl(AOwner);

  FLabel := nil; // No label by default
end;

constructor TListBoxEXT.Create(AOwner: TComponent; const LabelName: String;
  XPos, YPos, Width, Height: Integer);
begin
  Create(AOwner, LabelName, XPos, YPos);
  self.Width := Width;
  self.Height := Height;
end;

constructor TListBoxEXT.Create(AOwner: TComponent; const LabelName: String;
  XPos, YPos: Integer);
begin
  inherited Create(AOwner); // Call the base class constructor

  FDeltaY := -20;

  // Create and configure the label
  FLabel := TLabel.Create(AOwner);
  FLabel.Parent := TWinControl(AOwner);
  // Assign the label's Parent to the same Parent
  FLabel.Caption := LabelName;
  FLabel.Left := XPos;
  FLabel.Top := Max( YPos + FDeltaY,1);

  // Position the ListBox below the label
  self.Left := XPos;
  self.Top := YPos; // Add a small gap between the label and the ListBox
end;

destructor TListBoxEXT.Destroy;
begin
  if Assigned(FLabel) then
    FLabel.Free; // Free the label if it exists
  inherited; // Call the base destructor
end;

end.
Ich hatte eine Idee, wohin ich gehen wollte, und wollte mir das Kodieren bzw. Tippen mithilfe von KI ersparen. Mich würden andere Erfahrungsberichte im Umgang mit KI-generiertem Code interessieren. Ist 'KI-Code von der Stange' vielleicht weniger wartbar? Kann KI aktuell kein gutes Software-Design liefern?


Ich würde gerne die Euphorie teilen (z.B. dass Softwareentwickler bald überflüssig werden oder man durch KI-Unterstützung jetzt 30 % der Arbeit einsparen kann). Allerdings bin ich mir nicht sicher, ob ich heute wirklich etwas eingespart habe


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:38 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz