Jump to content
Lainkes

Create action for different components

Recommended Posts

Hello,

 

I have several DateTimePicker components.

At the start, I set these to an empty value.

When I click on one of these components, I want to execute the following code :

TDateTimePicker(Sender).Format := Formatsettings.ShortDateFormat;

If this is executed, the selected date is displayed. If not, it seems to stay empty.

 

I wrote a little procedure, with this code.

How can I like this to every component. I tried to link it to the 'onChange' event, but it's not listed.

 

Thanks for your advice.

 

Greetings

 

Lainkes

Share this post


Link to post

Why don't you set this property when creating the form? What is your reason for this?

Share this post


Link to post

I have a checkbox, a some other components on a line. Those components are disabled at the start.

This is repeated several times.

If a user clicks a checkbox (enabled), some components on the same line are enabled and my code is executed. Then a date can be selected and can be saved to the database.

If a user clicks a checkbox (disabled), some components on the same line are disabled and the value of the DateTimePicker is set to an empty value .

At the start of the program, the DateTimePicker components are empty, so no values are written to the databasse when no checkbox are enabled.

 

Maybe there is a better solution for this problem.

 

Greetings

 

Lainkes

 

2023-01-29 20_21_35-bim - Delphi 10.2 - frm_BIM [Built].png

Share this post


Link to post

I find that suggestion very confusing. I don't know anything about the background so I can't comment.
Always keep the user in mind.

  • will he understand it?
  • isn't it too complicated?
  • can't it be done differently?
  • why don't you use DB components there? At least in the beginning, until you learn to work with it

Share this post


Link to post

@Lainkes

you can try some like this, using a "container" to avoid many search for controls...

...
  private
    procedure MyCheckBoxOnClick(Sender: TObject);
...

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  for var i: integer := 0 to (ComponentCount - 1) do
    if (Components[i] is TCheckBox) then
      TCheckBox(Components[i]).OnClick := MyCheckBoxOnClick; // all checkbox with new OnClick...
end;

procedure TForm1.MyCheckBoxOnClick(Sender: TObject);
var
  MyParent: TPanel;
begin
  MyParent := TPanel(TCheckBox(Sender).Parent); // TPanel1,2,3
  //
  for var i: integer := 0 to (MyParent.ControlCount - 1) do
    if (MyParent.Controls[i] is TWinControl) and not(MyParent.Controls[i] = Sender) then
      TWinControl(MyParent.Controls[i]).Enabled := not TCheckBox(Sender).Checked;
end;

image.thumb.png.3b67e62c3dc31e28269b075a33515c51.png

Edited by programmerdelphi2k

Share this post


Link to post

Hi, i'm new, and i suggest to overload the TPanel component like this :

TPanel = class(Vcl.ExtCtrls.TPanel)
  private
    FUpdateEnabled: boolean;
    FChildControl: TComponent;
    FListeChild: TList;
    FUpdateVisibility: boolean;
    FUpdateVisibilityChilds: Boolean;
    function GetChildControl: TComponent;
    function GetListeChild: TList;
    function GetUpdateEnabled: Boolean;
    function GetUpdateVisible: Boolean;
    procedure SetChildControl(const Value: TComponent);
    procedure SetListeChild(const Value: TList);
    procedure SetUpdateEnabled(const Value: Boolean);
    procedure SetUpdateVisibility(const Value: Boolean);
  protected
    procedure CMVisibleChanged(var Message: TMessage); message CM_VISIBLECHANGED;
    procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
    procedure CMDNalUpdateVisibility(var Message: TMessage); message CM_CHILD_VISIBLECHANGED;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
    constructor Create(AOwner: TComponent); override;
    
  published
    property ChildControl: TComponent read FChildControl write FChildControl;    
    property ListeChild: TList read GetListeChild write SetListeChild;
    property UpdateEnabled: Boolean read GetUpdateEnabled write SetUpdateEnabled;
    property UpdateVisibility: Boolean read GetUpdateVisible write SetUpdateVisibility;
    property UpdateVisibilityChilds: Boolean read FUpdateVisibilityChilds write FUpdateVisibilityChilds;
  end;

And you can just use the UpdateEnabled properties to achieve that in ListeChild, just use the event EnabledChange for your component :

procedure TPanel.CMEnabledChanged(var Message: TMessage);
var i: integer;
begin
  inherited;
  if Self.FUpdateEnabled=false then exit;

  if not IsNil(Self.FChildControl) then
    TWinControl(Self.FChildControl).Enabled:= Self.Enabled;

  if (not isNil(Self.FListeChild)) and (Self.FListeChild.Count > 0) then
  begin
    for i := 0 to Self.FListeChild.Count-1 do
    begin
      if not IsNil(Self.FListeChild[i]) then
      TWinControl(Self.FListeChild[i]).Enabled:= Self.Enabled;
    end;
  end;
end;

 Hops that will help u;

Thanks

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×