CnPack Forum » CnVCL 组件包 » 关于CnOptionGroup使用的问题,请多多指教!


2009-2-19 17:01 雨中雪
关于CnOptionGroup使用的问题,请多多指教!

我创建一个类:以下是代码
unit GmConfig;

interface

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

type
  TGmConfig = class(TObject)
  private
   { Private declarations }
    FDate: TDate;
    FIntValue: Integer;
    procedure SetDate(const Value: TDate);
  public
    { Public declarations }
    property Date: TDate read FDate write SetDate;
    property IntValue: Integer read FIntValue write FIntValue;
  end;

implementation

procedure TGmConfig.SetDate(const Value: TDate);
begin
  FDate := Value;
end;

end.

以下是测试代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, CnAOTreeView, CnAutoOption,  RzButton,GmConfig;

type
  TForm1 = class(TForm)
    CnAOTreeView1: TCnAOTreeView;
    RzBitBtn1: TRzBitBtn;
    procedure RzBitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.RzBitBtn1Click(Sender: TObject);
var Option: TCnOptionGroup;
         gm:TGmConfig;
begin
  Option := TCnOptionGroup.Create(nil);
  gm:=TGmConfig.Create();
  gm.IntValue:=1;
  gm.Date:=Now;
  Option.Text := '自动参数设置演示程序';
  with Option.AddGroup('配置参数测试') do
  begin
[u]    AddItem(gm,'intvalue','数据');
    AddItem(gm,'date','日期');[/u]
  end;

  CnAOTreeView1.Options := Option;
end;

end.

现在的问题是运行下划线的语句时就出现错误,不知什么原因?请高手多多指教!

2009-2-19 19:39 zjy
请查看 Demo 的用法

Demo 在 cnpack\Examples\AutoOption 下面。

划线代码出错是因为 AutoOption 是通过 RTTI 来读写属性的,您需要:
1、从 TObject 派生来的 TGmConfig 默认是不带 RTTI 属性的,解决的办法一是从 TPersistent 派生,二是在类声明前用 {$M+} 编译指令指定生成 RTTI。
2、将 IntValue 和 Date 属性声明为 published 属性。

2009-2-20 12:29 雨中雪
type
  TGmConfig = class(TPersistent)
  private
   { Private declarations }
  public
    { Public declarations }
    FDate: TDate;
    FIntValue: Integer;

    procedure SetDate(const Value: TDate);
   
    property Date: TDate read FDate write SetDate;
    property IntValue: Integer read FIntValue write FIntValue;
  end;

按照您的说明,改了代码,为什么还不行

出现错误为:Invalid property define :TGmConfig.IntValue

2009-2-20 12:31 Passion
published

2009-2-20 12:36 雨中雪
感谢,问题已解决

页: [1]


Powered by Discuz! Archiver 5.0.0  © 2001-2006 Comsenz Inc.