Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

Property editor needed {worth 200 pts!}

I have a component that takes a TDataset
and formats its result to XML. I need a
property and property editor that will
allow the selection of which fields in
the dataset to use as Attributes. The
other fields not selected will be
presented as Elements in the resulting
XML document.

I'm thinking of having a dialog with a
TCheckListBox on it where the
AttributeFields can be checked.

This is the next-to-last thing I need
for this component and I don't know
where to begin on this one...

If you have specific questions, contact
me via e-mail at eshipman@e-mds.com...

Avatar of mullet_attack
mullet_attack

Here is an example of a simple component that has a non-standard property "Stuff", of type TStuff, a record.

Double-clicking TMyComponent invokes a component editor where you can edit the fields of Stuff.

Right-clicking has a context menu where some simple actions can occur.

Because TStuff is a non-standard property type, I've added TFiler code to stream the fields of TSTuff to/from the *.DFM

I'll post the code here in case other E-E people want to read it, and I'll e-mail the sample app to you.

----------------------------
TMyComponent
----------------------------
unit Mycomponent;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DsgnIntf,
  StuffEditor;

type
  TStuff = record
    Number : integer;
    Text : string[255];
  end;

  TMycomponent = class(tcomponent)
  private
    fStuff :TStuff;
    procedure SetStuff(const Value: TStuff);
    procedure DefineProperties(Filer:TFiler);override;
    procedure WriteStuffText(Writer:TWriter);
    procedure ReadStuffText(Reader:TReader);
    procedure WriteStuffNumber(Writer:TWriter);
    procedure ReadStuffNumber(Reader:TReader);
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    property Stuff : TStuff read FStuff write SetStuff;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Planetech', [TMycomponent]);
  RegisterComponentEditor(TMycomponent,TStuffEditor);
end;

{ TMycomponent }

procedure TMycomponent.DefineProperties(Filer: TFiler);
begin
  inherited;
  Filer.DefineProperty('Text', ReadStuffText, WriteStuffText, fStuff.text <> '');
  Filer.DefineProperty('Number', ReadStuffNumber, WriteStuffNumber, fStuff.number <> 0);
end;

procedure TMycomponent.ReadStuffNumber(Reader: TReader);
begin
  fStuff.Number := Reader.ReadInteger;
end;

procedure TMycomponent.ReadStuffText(Reader: TReader);
begin
  fStuff.Text := Reader.ReadString;
end;

procedure TMycomponent.SetStuff(const Value: TStuff);
begin
  FStuff := Value;
end;

procedure TMycomponent.WriteStuffNumber(Writer: TWriter);
begin
  Writer.WriteInteger(fStuff.Number);
end;

procedure TMycomponent.WriteStuffText(Writer: TWriter);
begin
  Writer.WriteString(fStuff.Text);
end;

end.

--------------------
TStuffEditor
--------------------
unit StuffEditor;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Spin, dsgnIntf;

type
  TStuffEditor = class(TComponentEditor)
  function GetVerbCount:integer;override;
  function GetVerb(index:integer):string;override;
  procedure ExecuteVerb(index:integer);override;
 end;

  TStuffDLG = class(TForm)
    Edit1: TEdit;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
  private
    { Private declarations }
  public
    function Execute(Component : TComponent) : boolean;
  end;

var
  StuffDLG: TStuffDLG;

implementation

uses MyComponent;

{$R *.DFM}

{ TStufEditor }

procedure TStuffEditor.ExecuteVerb(index: integer);
var
  tmpStuff : TStuff;
begin
  inherited;
  case index of
    0 :
      begin
        try
          StuffDlg := TStuffDlg.create(nil);
          if StuffDlg.execute(Component) then Designer.Modified;
        finally
          StuffDlg.free;
        end;
      end;
    1 :
      begin
        tmpStuff := TMyComponent(Component).Stuff;
        tmpStuff.Text := 'hello world';
        TMyComponent(Component).Stuff := tmpStuff;
        Designer.Modified;
      end;
    2 :
      begin
        tmpStuff := TMyComponent(Component).Stuff;
        tmpStuff.Text := 'foo';
        TMyComponent(Component).Stuff := tmpStuff;
        Designer.Modified;
      end;
  end;

end;

function TStuffEditor.GetVerb(index: integer): string;
begin
  case index of
    0:result := 'Open Dialog';
    1:result := 'StuffText := hello world';
    2:result := 'StuffText := foo';
  end;
end;

function TStuffEditor.GetVerbCount: integer;
begin
  result := 3;
end;

{ TStuffDLG }

function TStuffDLG.Execute(Component: TComponent): boolean;
var
  tmpStuff : TStuff;
begin
  tmpStuff := TMyComponent(Component).Stuff;
  edit1.text := tmpStuff.Text;
  spinedit1.value := tmpStuff.Number;
  ShowModal;
  result := ModalResult = mrOK;
  if result then
    begin
      tmpStuff.text := edit1.text;
      tmpStuff.number := spinedit1.value;
      TMyComponent(Component).Stuff := tmpStuff;
    end;
end;

end.

----------------
StuffEditor DFM
----------------
object StuffDLG: TStuffDLG
  Left = 335
  Top = 212
  Width = 321
  Height = 198
  Caption = 'Stuff Editor'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 24
    Top = 24
    Width = 37
    Height = 13
    Caption = 'Number'
  end
  object Label2: TLabel
    Left = 24
    Top = 88
    Width = 21
    Height = 13
    Caption = 'Text'
  end
  object Edit1: TEdit
    Left = 120
    Top = 80
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
  end
  object SpinEdit1: TSpinEdit
    Left = 120
    Top = 24
    Width = 121
    Height = 22
    MaxValue = 0
    MinValue = 0
    TabOrder = 1
    Value = 0
  end
  object Button1: TButton
    Left = 232
    Top = 136
    Width = 75
    Height = 25
    Caption = 'OK'
    ModalResult = 1
    TabOrder = 2
  end
end
Avatar of Eddie Shipman

ASKER

I need something more along the lines of the TFieldsEditor where I can build
a fieldlist of fields I want to write
as Attributes.

This is for strings only...
Well, Since I actually came up with my
own solution, I'll give you 100 points
for answering with a source solution.

I can't lower thepoints offered so I am deleting the question. If you'll
resubmit your answer, I'll accept it
for 100 points since you offered a
source soultion. I did, however, use
my own solution.

This question has a deletion request Pending
See above comment...
This question no longer is pending deletion
Eddie, You said you didn't know where to start on this one. I gave you a complete working example of a component editor 7 hours after you asked the question, and one day later you worked it out yourself.

If you didn't use my answer at all, then delete the question with no points given, otherwise accept this answer.
Cosidering, you posted a component editor .vs a property editor and I
needed a TStrings editor .vs an editor
for a Record, what do you think. I wrote
a TStrings PROPERTY editor and didn't
use any of your code at all. I'm
sending you a copy of the code to your
e-mail address to confirm it...

If you still want the 200 after seeing
my code, I'll approve it.

E
ASKER CERTIFIED SOLUTION
Avatar of mullet_attack
mullet_attack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well even your editor wasn't what I was looking for. If it had been a fields editor, sure, I would have probably used it. I couldn't see any way to conform it to what I was needing...

Sorry but I guess I didn't make myself clear enough...
Just to get rid of it all...