【问题标题】:design a custom checklist inherited from cxCheckList which contains functionalities of Orpheus checklistbox设计一个继承自 cxCheckList 的自定义清单,其中包含 Orpheus 清单框的功能
【发布时间】:2019-09-20 19:08:26
【问题描述】:

我正在尝试创建一个自定义清单组件,其中包含 Orpheus 组件 (TOvcCheckList) 的一些附加功能和一些 UI 增强功能。 我需要一些帮助来创建一个如图所示的 CheckList,

为了实现这个设计,到目前为止,我们还没有设计任何自定义组件,但我们尝试通过放置一个 cxGroupBox 并在组框顶部添加 cxCheckList 并实现该功能。 但是现在我们被要求创建一个组件,以便减少在每个地方编写功能的需要。

尝试以下实现设计的源代码如下。

unit DxSelectallGroupBox;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, cxContainer, cxEdit, cxCustomListBox,
  cxCheckListBox, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxPC, dxDockPanel, dxDockControl,
  Vcl.StdCtrls, cxGroupBox, cxCheckBox, dxBevel, System.ImageList, Vcl.ImgList,
  Vcl.CheckLst,Imagelistmodule;

type
  TDxSelectallGroupBox = class(TcxCustomGroupBox)
  private
    { Private declarations }
    fGBSelectAll: TcxGroupBox;
    fGBCheckList: TcxGroupBox;
    fCxCheckList: TcxCheckListBox;
    fDxBevel: TdxBevel;
    fCxCheckSelectAll: TcxCheckListBox;
  protected
    { Protected declarations }
  public
    { Public declarations }
     constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Test Components', [TDxSelectallGroupBox]);
end;

{ TDxChecklistGroupBox }

constructor TDxSelectallGroupBox.Create(AOwner: TComponent);
begin
   inherited;
  SetBounds(Left, Top, 140, 120);
  fGBSelectAll := TcxGroupBox.Create(self);
  fGBSelectAll.Parent := Twincontrol(AOwner);
  fGBSelectAll.SetBounds(10, 10, width, 185);
  fGBSelectAll.Align := alNone;
  fGBSelectAll.Alignment := alTopLeft;

  fCxCheckSelectAll := TcxCheckListBox.Create(Self);
  fCxCheckSelectAll.Parent := fGBSelectAll;

  FdxBevel := Tdxbevel.Create(Self);
  FdxBevel.Parent := fGBSelectAll;

  FgbCheckList := TcxGroupBox.Create(Self);
  FgbCheckList.Parent := fGBSelectAll;

  fCxCheckList := TcxCheckListBox.Create(Self);
  fCxCheckList.Parent := FgbCheckList;

  with fGBSelectAll do begin
    PanelStyle.Active := True;
    ParentBackground := False;
    ParentColor := False;
    Style.BorderColor := 15065047;
    Style.BorderStyle := ebsSingle;
    Style.Color := clWhite;
    Style.LookAndFeel.NativeStyle := False;
    TabOrder := 0;
  end;
    with FdxBevel do begin
      Left := 2;
      Top := 38;
      Width := 181;
      Height := 1;
      Align := alTop;
      AutoSize := True;
      LookAndFeel.NativeStyle := False;
    end ;
    with FgbCheckList do begin
      Left := 2 ;
      Top := 39;
      Align := alClient;
      PanelStyle.Active := True;
      ParentBackground := False;
      Style.BorderStyle := ebsNone;
      Style.LookAndFeel.NativeStyle := False;
      StyleDisabled.LookAndFeel.NativeStyle := False;
      TabOrder := 0;
      Height := 121;
      Width := 181;
    end;
      with fCxCheckList do begin
        Left := 2;
        Top := 2;
        Width := 177;
        Height := 117;
        Margins.Left := 5;
        Margins.Top := 0;
        Margins.Right := 0;
        Margins.Bottom := 0;
        Align := alClient;
        ParentFont := False;
        Style.BorderStyle := cbsNone;
        Style.Color := clWhite;
        Style.Font.Charset := ANSI_CHARSET;
        Style.Font.Color := 7697781;
        Style.Font.Height := -16;
        Style.Font.Name := 'Noto Sans';
        Style.Font.Style := [];
        Style.HotTrack := True;
        Style.LookAndFeel.NativeStyle := False;
        TabOrder := 0;
        additem('One');
        additem('Two');
        additem('Three');
        showchecks:=true;
    end;
    with fCxCheckSelectAll do
    begin
      AlignWithMargins := True;
      Left := 5;
      Top := 5 ;
      Width := 175 ;
      Height := 30 ;
      Align := alTop;
      ParentFont := False ;
      Style.BorderStyle := cbsNone;
      Style.Font.Charset := ANSI_CHARSET;
      Style.Font.Color := 7697781;
      Style.Font.Height := -16 ;
      Style.Font.Name := 'Noto Sans';
      Style.Font.Style := [] ;
      Style.LookAndFeel.NativeStyle := False;
      StyleDisabled.BorderStyle := cbsNone;
      TabOrder := 1 ;
      additem('Select All');
      Showchecks :=True;
    end;


end;

end.

安装组件后,我得到如下所示

任何人都可以帮助纠正我的来源并让我朝着正确的方向前进吗? 谢谢。

更新

我找到了错误的根本原因并修复了Control '' has no parent window。当我安装并尝试将组件放在表单上时,它会给出类似的错误 Access violation at address 1D405F2E in module 'cxLibraryRS25.bpl'. Write of'地址00000090`

【问题讨论】:

标签: delphi devexpress vcl delphi-10.2-tokyo checklistbox


【解决方案1】:
fGBSelectAll.Parent := Twincontrol(AOwner);

您将fGBSelectAll 放在复合控件的所有者身上。该所有者通常是表单。因此,不是将子组框放在主组框中,而是将它放在它之外,直接放在表单上。

解决方案:使用Self,它指的是TDxSelectallGroupBox实例本身的实例。

fGBSelectAll.Parent := Self;

【讨论】:

  • 我尝试了同样的方法但是当我将组件放在表单上时出现错误错误是Control '' has no parent window.我是否必须将所有这些语句更改为self' fCxCheckSelectAll := TcxCheckListBox.Create(Self ); ' ` fCxCheckSelectAll.Parent := fGBSelectAll; ` ` FdxBevel := Tdxbevel.Create(Self); ` ` FdxBevel.Parent := fGBSelectAll; ` ` FgbCheckList := TcxGroupBox.Create(Self); ` ` FgbCheckList.Parent := fGBSelectAll; ` ` fCxCheckList := TcxCheckListBox.Create(Self); ` ` fCxCheckList.Parent := FgbCheckList;`
  • 我觉得其他都还好。关于错误,它与父级尚未处理有关。您可以将父项的设置或控件的整个创建移动到另一种方法。这在this questionthis question 中得到了证明。长话短说,您可以覆盖LoadedSetParent,甚至CreateWindowHandle,以便在正确的时间设置子组件的父级。
【解决方案2】:

下面的代码是根据需要创建组件

unit DxSelectallGroupBox;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, cxContainer, cxEdit, cxCustomListBox,
  cxCheckListBox, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxPC, dxDockPanel, dxDockControl,
  Vcl.StdCtrls, cxGroupBox, cxCheckBox, dxBevel, System.ImageList, Vcl.ImgList,
  Vcl.CheckLst, cxListView;

type
  TDxSelectallGroupBox = class(TcxCustomGroupBox)
  private
    { Private declarations }
    fGBSelectAll: TcxGroupBox;
    fGBCheckList: TcxGroupBox;
    fCxCheckList: TcxCheckListBox;
    fDxBevel: TShape;
    fCxCheckSelectAll: TcxCheckBox;
  protected
    { Protected declarations }
  public
    { Public declarations }
     constructor Create(AOwner: TComponent); override;         
  published
    { Published declarations }

  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Test Components', [TDxSelectallGroupBox]);
end;

{ TDxChecklistGroupBox }

constructor TDxSelectallGroupBox.Create(AOwner: TComponent);
begin
  inherited;
  Parent := TWinControl(AOwner);
  SetBounds(Left, Top, 140, 120);
  Height := 200;
  Width := 200;
  fGBSelectAll := TcxGroupBox.Create(self);
  fGBSelectAll.Parent := self;
  fGBSelectAll.SetBounds(1, 1, width, 199);
  fGBSelectAll.Align := alNone;
  fGBSelectAll.Alignment := alTopLeft;

  fCxCheckSelectAll := TcxCheckBox.Create(Self);
  fCxCheckSelectAll.Parent := fGBSelectAll;

  FdxBevel := TShape.Create(Self);
  FdxBevel.Parent := fGBSelectAll;

  FgbCheckList := TcxGroupBox.Create(Self);
  FgbCheckList.Parent := fGBSelectAll;

  fCxCheckList := TcxCheckListBox.Create(Self);
  fCxCheckList.Parent := FgbCheckList;

  Style.BorderColor := 15065047;
  Style.BorderStyle := ebsSingle;
  Style.Color := clWhite;
  Style.LookAndFeel.NativeStyle := False;

  with fGBSelectAll do begin
    PanelStyle.Active := True;
    ParentBackground := False;
    ParentColor := False;
    Style.BorderColor := 15065047;
    Style.BorderStyle := ebsSingle;
    Style.Color := clWhite;
    Style.LookAndFeel.NativeStyle := False;
    TabOrder := 0;
    Width := 199;
  end;
    with FdxBevel do begin
      Left := 0;
      Top := 38;
      Width :=300;
      Height := 1;
      Align := alTop;
      Pen.Color := 15065047;
    end ;
    with FgbCheckList do begin
      Left := 2 ;
      Top := 39;
      Align := alClient;
      PanelStyle.Active := True;
      ParentBackground := False;
      Style.BorderStyle := ebsNone;
      Style.LookAndFeel.NativeStyle := False;
      StyleDisabled.LookAndFeel.NativeStyle := False;
      TabOrder := 0;
      Height := 200;
      Width := 200;
    end;
      with fCxCheckList do begin
        Left := 2;
        Top := 2;
        Width := 200;
        Height := 117;
        Margins.Left := 5;
        Margins.Top := 0;
        Margins.Right := 0;
        Margins.Bottom := 0;
        Align := alClient;
        ParentFont := False;
        Style.BorderStyle := cbsNone;
        Style.Color := clWhite;
        Style.Font.Charset := ANSI_CHARSET;
        Style.Font.Color := 7697781;
        Style.Font.Height := -16;
        Style.Font.Name := 'Noto Sans';
        Style.Font.Style := [];
        Style.HotTrack := True;
        Style.LookAndFeel.NativeStyle := False;
        StyleFocused.BorderStyle := cbsNone;
        StyleHot.BorderStyle := cbsNone;
        TabOrder := 0;
        additem('One');
        additem('Two');
        additem('Three');
        showchecks:=true;
    end;
    with fCxCheckSelectAll do
    begin
      AlignWithMargins := True;
      Left := 5;
      Top := 5 ;
      Width := 200 ;
      Height := 30 ;
      Align := alTop;
      ParentFont := False ;
      Style.BorderStyle := ebsSingle;
      Style.BorderColor := 15065047;
      Style.Font.Charset := ANSI_CHARSET;
      Style.Font.Color := 7697781;
      Style.Font.Height := -16 ;
      Style.Font.Name := 'Noto Sans';
      Style.Font.Style := [] ;
      Style.LookAndFeel.NativeStyle := False;
      StyleDisabled.BorderStyle := ebsSingle;
      StyleHot.BorderStyle := ebsSingle;
      TabOrder := 1 ;
      Caption := 'Select All';
    end;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多