Home Component Writing

help for creating my first component: TCustomDBCalc

edited October 2002 in Component Writing
I need to create a custom DBCalc component.
The DBCalc component included in RBPro only let end-users to SUM, COUNT,
AVERAGE, etc...
I want my component to:
- PERCENTAJE
- SUM X
- MULTIPLY BY X
- DIVIDE BY X

So, if the text of the DBCalc is "100", and enjd-user selects PERCENTAJE 30,
the result will be "30".
Or, if end-user selects SUM 200, result will by "200"

Where can I find an example of custom components with custom dialogs?
Thanks!
PD: My component will be free :-)

Comments

  • edited October 2002
    The checkbox demo in the RBuilder\Demos\RCL directory shows how to create a
    custom component and register it for use at both Delphi design time and at
    runtime. Here is an example for a custom shape which has a custom dialog to
    control the line thickness.

    http://www.digital-metaphors.com/tips/SetShapePen.zip


    Cheers,

    Jim Bennett
    Digital Metaphors

  • edited October 2002
    It is going to be easier to descend from a component and extend it. You can
    override the CreatePopupMenu method to add a menu option for the new
    published property. Then you can launch a custom dialog in response to the
    user clicking on the menu item for the new property, as shown in the custom
    shape example.


    Cheers,

    Jim Bennett
    Digital Metaphors

  • edited October 2002
    Thanks, Jim... BUT...

    I don't know which is the best method to do this:
    1. Add a custom property and dialog to all TppCustomText derived components
    (DBText, DBCalc, etc...)
    2. Create a new component

    Which option is easier (or possible)?


  • edited October 2002
    I have found the TppCustomText.CreatePopupMenu procedure, but I don't know
    hot to add the new popup-option...

    I want my option be visible on both PPDBText and PPDBCalc PopUpMenus...
    Where do I add the aPopupMenu.AddItem() line?

    Wich parameters do I have to add?
    aPopupMenu.AddItem(30, 'AdvancedCalc', '', ????);

    I think the last parameter is the one who say RB wich caption will have the
    menu option, depending on the language ID. Isn't it?
    I only want my menu-option available in Spanish, so Can I write manually and
    directly the caption?

    Can you help me, please? I have never made a component...
    Thanks!




  • edited October 2002
    Don't change the ancestor. You should create two descendents instead.
    UnRegister the default TppDBText and TppDBCalc classes, as shown in the
    finalization call for thos components. Then you can register two new
    descendents which have overridden the CreatePopupMenu item.

    The SetShapePen example provided in a previous post shows how to add a menu
    item to the popup menu. Yes, you can directly enter the caption in Spanish.
    Here is the code in the demo that creates a menu item.

    {---------------------------------------------------------------------------
    ---}
    { TmyShape.CreatePopupMenu }

    procedure TmyShape.CreatePopupMenu(aOwner: TComponent; var aPopupMenu:
    TppPopupMenu);
    var
    lMenuItem: TMenuItem;
    begin

    {creates based options (BringToFront, SendToBack, Position, Visible) }
    inherited CreatePopupMenu(aOwner, aPopupMenu);

    {TppPopupMenu is a descendant of TPopupMenu. It has added functionality
    for
    adding menu items, referencing menu items by name, and handling menu
    items
    that represent boolean properties.}


    lMenuItem := TMenuItem.Create(aPopupMenu);
    lMenuItem.Name := 'LineThickness';
    lMenuItem.Caption := 'Line Thickness...';
    lMenuItem.Tag := aPopupMenu.Items.Count;

    aPopupMenu.Items.Add(lMenuItem);

    end; {procedure, TmyShape}


    Cheers,

    Jim Bennett
    Digital Metaphors

  • edited October 2002
    OK, Thanks, Jim...

    I was able to get it working! BUT only in design time!

    I have the following DBCustomCalc descending from "ppDBTExt".
    In my CustomDialog I have a SpinEdit (with the value of Value property) and
    a ComboBox, which determines wich Operation will make.

    procedure TDBCustomCalc.MenuItemClickEvent(Sender: TObject);
    var
    CustomCalcDialog: TMyCustomCalcDialog;
    begin
    CustomCalcDialog := TMyCustomCalcDialog.Create(nil);
    CustomCalcDialog.Showmodal;
    case CustomCalcDialog.Operation of
    0 : caption : = floattostr(strtofloat(caption)+CustomCalcDialog.Value);
    1 : caption : = floattostr(strtofloat(caption)-CustomCalcDialog.Value);
    2 : caption : = floattostr(strtofloat(caption)*CustomCalcDialog.Value);
    3 : caption : = floattostr(strtofloat(caption)/CustomCalcDialog.Value);
    4 : caption : =
    floattostr((strtofloat(caption)*CustomCalcDialog.Value)/100);
    end;
    CustomCalcDialog.Free;
    end;

    In design time, I put a DBText and my custom component toguether. Yes, there
    is an option menu. I click on it and.. YES, there is my Dialog!
    Y select Operation and Value, and YES! I can see (in the designer, not
    preview) the result value!
    BUT when I preview the report, it only shows the original value of the
    dbtext.

    I think I have to override another procedure, like OnGetText, OnPrint or
    something. Isn't it?



  • edited October 2002
    There is a virtual Compute method which is where the value is "computed."
    You can override this method to provide your custom calculations.


    Cheers,

    Jim Bennett
    Digital Metaphors

This discussion has been closed.