Einzelnen Beitrag anzeigen

bernhard_LA

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

AW: KI Code verbessern, ListBox mit Label

  Alt 28. Jul 2025, 12:32
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;
  Mit Zitat antworten Zitat