Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#6

Re: Spalten und zeilen zur laufzeit

  Alt 8. Aug 2006, 17:32
Schau mal her:

Delphi-Quellcode:
procedure CreateButtonField(s: TStrings; wcParent: TWinControl; maxRows: Integer;
  onClick: TNotifyEvent);
const
  BTN_WIDTH = 100;
  BTN_HEIGHT = 40;
  TOP_MARGIN = 10;
  LEFT_MARGIN = 10;
  VERT_SPACE = 10;
  HORZ_SPACE = 10;
var
  i, iCol, iRow: Integer;
  btn: TButton;
begin
  for i := 0 to Pred(s.Count) do
  begin
    iCol := i div maxRows;
    iRow := i mod maxRows;
    btn := TButton.Create(wcParent.Owner);
    btn.Caption := s[i];
    btn.Width := BTN_WIDTH;
    btn.Height := BTN_HEIGHT;
    btn.Parent := wcParent;
    btn.Top := TOP_MARGIN + (BTN_HEIGHT + VERT_SPACE) * iRow;
    btn.Left := LEFT_MARGIN + (BTN_WIDTH + HORZ_SPACE) * iCol;
    btn.OnClick := onClick;
    s.Objects[i] := btn;
  end;
end;

procedure TMainForm.InitButtonClick(Sender: TObject);
var
  i: Integer;
begin
  with ListBox do
  begin
    for i := 0 to Pred(Items.Count) do
      TButton(Items.Objects[i]).Free;
    Clear;
    for i := 0 to 20 + Random(5) do
      ListBox.Items.Add(IntToStr(i));
  end;

  CreateButtonField(ListBox.Items, Panel, 5, DemoButtonClick);
end;

procedure TMainForm.DemoButtonClick(Sender: TObject);
begin
  with Sender as TButton do
    ShowMessage(Caption);
end;
Grüße vom marabu
  Mit Zitat antworten Zitat