Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#20

AW: Zur Laufzeit Comboboxen hinzufügen

  Alt 15. Feb 2020, 13:33
Ein neuer Versuch mit ein paar Zugaben, schönes Wochenende und diesmal wurden meinerseits auch Mängel berücksichtigt.

Viel Spass beim rumtesten.

Die .pas Datei:
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    cbPnl: TPanel;
    mainPnl: TPanel;
    infoPnl: TPanel;
    addBtn: TButton;
    procedure addBtnClick(Sender: TObject);
  private
  public
    // manuell eingefügte events die ich später verwende
    procedure cbChange(Sender: TObject);
    procedure cbClick(Sender: TObject);
  end;

var
  Form1: TForm1;
  cbList: TList;

const
  minHeight = 120;

implementation

{$R *.dfm}

procedure TForm1.addBtnClick(Sender: TObject);
var
  cb : TComboBox;
begin
  // hier wird eine neue combobox erstellt mit ein paar grundeinstellungen
  // in diesem fall erstelle ich die cb in ein panel
  cb := TComboBox.Create(cbPnl);
  cb.Parent := Self;
  cb.Left := 3;
  cb.Top := cbList.Count * cb.Height + 3;
  cb.Style := csDropDownList;
  // damit man auch was zu sehen/klicken hat, ein paar einträge erzeugen
  cb.Items.Add('CB #' + IntToStr(cbList.Count));
  cb.Items.Add('Cool #' + IntToStr(cbList.Count));
  cb.ItemIndex := 0;
  // hier weise ich der cb events zu, für weitere events einfach diesem beispiel folgen
  cb.OnChange := cbChange;
  cb.OnClick := cbClick;
  // verwaltung der cb's
  cbList.Add(cb);
  // prototyp einer dynamischen formular größe
  if ((cbList.Count + 2) * cb.Height + 3) > minHeight then
    Height := (cbList.Count + 2) * cb.Height + 3;
end;

procedure TForm1.cbChange(Sender: TObject);
begin
  infoPnl.Caption := TComboBox(Sender).Text;
end;

procedure TForm1.cbClick(Sender: TObject);
begin
  infoPnl.Caption := TComboBox(Sender).Text;
end;

initialization
  cbList := TList.Create;

finalization
  cbList.Free;

end.
die .dfm Datei:
Delphi-Quellcode:
object Form1: TForm1
  Left = 192
  Top = 125
  BorderIcons = [biSystemMenu]
  BorderStyle = bsSingle
  Caption = 'DynCbDemo by KodeZwerg'
  ClientHeight = 81
  ClientWidth = 315
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object cbPnl: TPanel
    Left = 0
    Top = 0
    Width = 150
    Height = 81
    Align = alLeft
    TabOrder = 0
  end
  object mainPnl: TPanel
    Left = 150
    Top = 0
    Width = 165
    Height = 81
    Align = alClient
    TabOrder = 1
    object infoPnl: TPanel
      Left = 1
      Top = 1
      Width = 163
      Height = 41
      Align = alTop
      BevelOuter = bvNone
      Caption = 'CB Item Display'
      TabOrder = 0
    end
    object addBtn: TButton
      Left = 16
      Top = 48
      Width = 129
      Height = 25
      Caption = 'Add ComboBox'
      TabOrder = 1
      OnClick = addBtnClick
    end
  end
end
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat