在运行时绘制TPanel和TSplitter会导致组件顺序错误

时间:2014-09-12 08:39:43

标签: delphi delphi-xe5

如果我在运行时将多个TPanel和TSplitter组件创建到TScrollBox中,则组件的顺序是错误的。如果我调用drawInput()3次,则滚动框包含3个面板,后跟3个分割器,而不是1个面板,后跟1个分割器(重复)。

如何强制执行正确的订单?

Here is a screenshot

function drawInput(owner: TWinControl): TWinControl;
var
  panel: TPanel;
  edit: TEdit;
  splitter: TSplitter;
begin
  panel := TPanel.Create(owner);
  panel.Parent := owner;
  panel.Align := alTop;
  panel.AlignWithMargins := True;
  panel.BorderWidth := 0;
  panel.Color := clRed;
  panel.BorderStyle := bsNone;
  panel.ParentBackground := False;
  panel.Ctl3D := False;

  edit := TEdit.Create(panel);
  edit.Parent := panel;
  edit.Align := alTop;
  edit.AlignWithMargins := True;
  edit.Text := 'foo';
  edit.Margins.Left := 5;
  edit.Margins.top := 5;
  edit.Margins.Bottom := 5;
  edit.Margins.Right := 5;

  splitter := TSplitter.Create(owner);
  splitter.Parent := owner;
  splitter.Align := alTop;
  splitter.Beveled := True;
  splitter.Height := 3;
end;

修改

以下是我如何调用此函数:

procedure TForm2.Button1Click(Sender: TObject);
var
  form: TForm;
  sb: TScrollBox;
begin
  form := TForm.Create(Application);
  sb := TScrollBox.Create(form);
  sb.Parent := form;
  sb.Align := alClient;
  sb.Color := clBlack;
  drawInput(sb);
  drawInput(sb);
  drawInput(sb);
  drawInput(sb);

  form.Width := 300;
  form.Height := 700;
  form.ShowModal;
end;

3 个答案:

答案 0 :(得分:0)

Delphi的对齐逻辑有时很难。但以下工作。请注意第splitter.Top := -1;

function drawInput(owner: TWinControl): TWinControl;
    var
      panel: TPanel;
      edit: TEdit;
      splitter: TSplitter;
    begin
      splitter := TSplitter.Create(owner);
      splitter.Parent := owner;
      splitter.Align := alTop;
      splitter.Beveled := True;
      splitter.Height := 3;
      splitter.Top := -1;

      panel := TPanel.Create(owner);
      panel.Parent := owner;
      panel.Align := alTop;
      panel.AlignWithMargins := True;
      panel.BorderWidth := 0;
      panel.Color := clRed;
      panel.BorderStyle := bsNone;
      panel.ParentBackground := False;
      panel.Ctl3D := False;

      edit := TEdit.Create(panel);
      edit.Parent := panel;
      edit.Align := alTop;
      edit.AlignWithMargins := True;
      edit.Text := 'foo';
      edit.Margins.Left := 5;
      edit.Margins.top := 5;
      edit.Margins.Bottom := 5;
      edit.Margins.Right := 5;
    end;

enter image description here

答案 1 :(得分:0)

放置面板+分割器,然后设置对齐 您可以通过将面板与客户端对齐来将面板放在所有其他组件下面

function drawInput(owner: TWinControl): TWinControl;
var
  panel: TPanel;
  edit: TEdit;
  splitter: TSplitter;
begin
  panel := TPanel.Create(owner);
  panel.Parent := owner;
  ///
  panel.Align := alclient;
  ///
  panel.Align := alTop;
  panel.AlignWithMargins := True;
  panel.BorderWidth := 0;
  panel.Color := clRed;
  panel.BorderStyle := bsNone;
  panel.ParentBackground := False;
  panel.Ctl3D := False;    


  splitter := TSplitter.Create(owner);
  splitter.Parent := owner;
  //
  splitter.top := panel.top+panel.height;
  //
  splitter.Align := alTop;
  splitter.Beveled := True;
  splitter.Height := 3;
end;

答案 2 :(得分:0)

这是在XE5上适合我的代码。我仍然需要解决我的问题,但至少我修复了你的问题:)

procedure drawInput(owner: TWinControl; var t: integer);
var
  panel: TPanel;
  edit: TEdit;
  splitter: TSplitter;
begin
  panel := TPanel.Create(owner);
  panel.Parent := owner;
  panel.Align := alTop;
  panel.AlignWithMargins := True;
  panel.BorderWidth := 0;
  panel.Color := clRed;
  panel.BorderStyle := bsNone;
  panel.ParentBackground := False;
  panel.Ctl3D := False;
  panel.Top := t;
  t := panel.Top + panel.Height + 1;

  edit := TEdit.Create(panel);
  edit.Parent := panel;
  edit.Align := alTop;
  edit.AlignWithMargins := True;
  edit.Text := 'foo';
  edit.Margins.Left := 5;
  edit.Margins.top := 5;
  edit.Margins.Bottom := 5;
  edit.Margins.Right := 5;

  splitter := TSplitter.Create(owner);
  splitter.Parent := owner;
  splitter.Align := alTop;
  splitter.Beveled := True;
  splitter.Height := 3;
  splitter.Top := t;

  t := splitter.Top + splitter.Height + 1;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  form: TForm;
  sb: TScrollBox;
  t: integer;
begin
  form := TForm.Create(Application);
  sb := TScrollBox.Create(form);
  sb.Parent := form;
  sb.Align := alClient;
  sb.Color := clBlack;
  t := 0;
  drawInput(sb, t);
  drawInput(sb, t);
  drawInput(sb, t);
  drawInput(sb, t);

  form.Width := 300;
  form.Height := 700;
  form.ShowModal;
end;