Showing posts with label property. Show all posts
Showing posts with label property. Show all posts

Tuesday, 19 June 2018

Why does TSpinEdit not enforce the MaxValue property?

With a TSpinEdit there is a property called 'MaxValue', from the name it implies that the maximum value for the control is what value you set for the property, but this is not the case. The property sets the maximum value for using the spin edit buttons, a user can enter a value exceeding the maximum value by using the keyboard up to the value of an Int64 (9,223,372,036,854,775,807).

This seems to have been done by design, but I cannot see a valid reason for this and it could be a little dangerous when being used to control the valid values entered.  

By putting the following in the OnChange event I've managed to get around the problem. Not sure if there is a better solution. 

if (Sender is TSpinEdit) then
begin
    if (Sender as TSpinEdit).Value > (Sender as TSpinEdit).MaxValue then
    begin
        (Sender as TSpinEdit).Value := (Sender as TSpinEdit).MaxValue;
    end;
end;



Thursday, 25 May 2017

Frame Inheritance - TabOrder: Property does not exist - Issue Fixed

I've been working on an application recently and it has a few frames (TFrame), I noticed that there were common properties and methods to all the frames, so I decided that they should derive from a base class. I created the base frame class and then in code derived the other frames from the base frame. It all seemed to work fine a build and ran OK, however after closing the project and then coming back to it a few days later, when I tried to open a frame I received an error stating the TabOrder property does not exist, I ignored it and received more property does not exist errors (I did not save any changes). The solution to this problem was simple, in the .dfm file at the top was:

object TestFrame: TTestFrame 

This needed to be changed to:

inherited TestFrame: TTestFrame

The ideal way to do this is at design time when designing the structure of the application, if there is an abstract or concrete frame class then this should be added to the repository so when a developer needs to create a new frame, they can simply inherit the frame from the one in the repository.

This is a very similar problem to inheriting datamodules, I've also had to do something similar. 

Thursday, 28 February 2013

Iterate through controls on a form

Here is how to iterate through all the controls on a form, this is very useful for setting a property value of more than one control, for example the enabled property.

procedure IterateControls(AControl: TControl);
    var
        iControl: Integer;
    begin
        if AControl = nil then
            Exit;
        if AControl is TWinControl then
        begin
            for iControl := 0 to TWinControl(AControl).ControlCount - 1 do
                IterateControls(TWinControl(AControl).Controls[iControl]);
        end;

        // Do Control stuff here
        AControl.Enabled := false;
    end;

Where you want to set the control property value it is possible to check for the class something like this.

if (AControl is TButton) then
   (AControl as TButton).Enabled := false;

And, of course the boolean value can be passed as a parameter to the procedure.